This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: Implicit conversion and scalac

5 replies
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.

two

On 28 Dec 2011, at 14:06, Edmondo Porcu wrote:

> implicit def toRichCurrency(currency:Currency) =
> new {
> def hello = println("hello");
> def hi = println("hi");
> }
>
> def myMethod(currency:Currency){
> currency.hello
> currency.hi
> }
>
> One or two conversions?
>
> Best Regards
>
> 2011/12/28 Sciss
> if your pimp'ed Int's operators return an Int, you will see three conversions.
>
> On 28 Dec 2011, at 14:00, Edmondo Porcu wrote:
>
> > Dear All,
> > how does the scala compiler handles implicit conversions?
> >
> > If in the same method I access 3 methods of PimpMyInt on the same Int object, will the conversion be executed only once?
> >
> > Best Regards
> >
> > Edmondo
>
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Implicit conversion and scalac

the compiler cannot reuse the enrichment instance because right now it is not smart enough to see that the enrichment instance has no state.

-------- Original-Nachricht --------
> Datum: Wed, 28 Dec 2011 14:11:15 +0100
> Von: Sciss
> An: Edmondo Porcu , scala-user
> Betreff: Re: [scala-user] Implicit conversion and scalac

> two
>
> On 28 Dec 2011, at 14:06, Edmondo Porcu wrote:
>
> > implicit def toRichCurrency(currency:Currency) =
> > new {
> > def hello = println("hello");
> > def hi = println("hi");
> > }
> >
> > def myMethod(currency:Currency){
> > currency.hello
> > currency.hi
> > }
> >
> > One or two conversions?
> >
> > Best Regards
> >
> > 2011/12/28 Sciss
> > if your pimp'ed Int's operators return an Int, you will see three
> conversions.
> >
> > On 28 Dec 2011, at 14:00, Edmondo Porcu wrote:
> >
> > > Dear All,
> > > how does the scala compiler handles implicit conversions?
> > >
> > > If in the same method I access 3 methods of PimpMyInt on the same Int
> object, will the conversion be executed only once?
> > >
> > > Best Regards
> > >
> > > Edmondo
> >
> >
>

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Implicit conversion and scalac

but the VM may well be able to erase the penalty of instantiation altogether, so in the spirit of the recent micro optimization discussions, i would say, don't waste your time thinking about it _unless you encounter a real performance problem related to it_.

On 28 Dec 2011, at 14:16, Dennis Haupt wrote:

> the compiler cannot reuse the enrichment instance because right now it is not smart enough to see that the enrichment instance has no state.
>
>
>
> -------- Original-Nachricht --------
>> Datum: Wed, 28 Dec 2011 14:11:15 +0100
>> Von: Sciss
>> An: Edmondo Porcu , scala-user
>> Betreff: Re: [scala-user] Implicit conversion and scalac
>
>> two
>>
>> On 28 Dec 2011, at 14:06, Edmondo Porcu wrote:
>>
>>> implicit def toRichCurrency(currency:Currency) =
>>> new {
>>> def hello = println("hello");
>>> def hi = println("hi");
>>> }
>>>
>>> def myMethod(currency:Currency){
>>> currency.hello
>>> currency.hi
>>> }
>>>
>>> One or two conversions?
>>>
>>> Best Regards
>>>
>>> 2011/12/28 Sciss
>>> if your pimp'ed Int's operators return an Int, you will see three
>> conversions.
>>>
>>> On 28 Dec 2011, at 14:00, Edmondo Porcu wrote:
>>>
>>>> Dear All,
>>>> how does the scala compiler handles implicit conversions?
>>>>
>>>> If in the same method I access 3 methods of PimpMyInt on the same Int
>> object, will the conversion be executed only once?
>>>>
>>>> Best Regards
>>>>
>>>> Edmondo
>>>
>>>
>>

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Implicit conversion and scalac

( p.s. although probably it is advisable to make a real pimped class with methods 'hello' and 'hi', since with the code below you end up with structural types, no? )

On 28 Dec 2011, at 14:42, Sciss wrote:

> but the VM may well be able to erase the penalty of instantiation altogether, so in the spirit of the recent micro optimization discussions, i would say, don't waste your time thinking about it _unless you encounter a real performance problem related to it_.
>
> On 28 Dec 2011, at 14:16, Dennis Haupt wrote:
>
>> the compiler cannot reuse the enrichment instance because right now it is not smart enough to see that the enrichment instance has no state.
>>
>>
>>
>> -------- Original-Nachricht --------
>>> Datum: Wed, 28 Dec 2011 14:11:15 +0100
>>> Von: Sciss
>>> An: Edmondo Porcu , scala-user
>>> Betreff: Re: [scala-user] Implicit conversion and scalac
>>
>>> two
>>>
>>> On 28 Dec 2011, at 14:06, Edmondo Porcu wrote:
>>>
>>>> implicit def toRichCurrency(currency:Currency) =
>>>> new {
>>>> def hello = println("hello");
>>>> def hi = println("hi");
>>>> }
>>>>
>>>> def myMethod(currency:Currency){
>>>> currency.hello
>>>> currency.hi
>>>> }
>>>>
>>>> One or two conversions?
>>>>
>>>> Best Regards
>>>>
>>>> 2011/12/28 Sciss
>>>> if your pimp'ed Int's operators return an Int, you will see three
>>> conversions.
>>>>
>>>> On 28 Dec 2011, at 14:00, Edmondo Porcu wrote:
>>>>
>>>>> Dear All,
>>>>> how does the scala compiler handles implicit conversions?
>>>>>
>>>>> If in the same method I access 3 methods of PimpMyInt on the same Int
>>> object, will the conversion be executed only once?
>>>>>
>>>>> Best Regards
>>>>>
>>>>> Edmondo
>>>>
>>>>
>>>
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Implicit conversion and scalac

structural types are ok for this

-------- Original-Nachricht --------
> Datum: Wed, 28 Dec 2011 14:46:47 +0100
> Von: Sciss
> An: Sciss
> CC: "Dennis Haupt" , scala-user [at] googlegroups [dot] com, edmondo [dot] porcu [at] gmail [dot] com
> Betreff: Re: [scala-user] Implicit conversion and scalac

> ( p.s. although probably it is advisable to make a real pimped class with
> methods 'hello' and 'hi', since with the code below you end up with
> structural types, no? )
>
> On 28 Dec 2011, at 14:42, Sciss wrote:
>
> > but the VM may well be able to erase the penalty of instantiation
> altogether, so in the spirit of the recent micro optimization discussions, i
> would say, don't waste your time thinking about it _unless you encounter a real
> performance problem related to it_.
> >
> > On 28 Dec 2011, at 14:16, Dennis Haupt wrote:
> >
> >> the compiler cannot reuse the enrichment instance because right now it
> is not smart enough to see that the enrichment instance has no state.
> >>
> >>
> >>
> >> -------- Original-Nachricht --------
> >>> Datum: Wed, 28 Dec 2011 14:11:15 +0100
> >>> Von: Sciss
> >>> An: Edmondo Porcu , scala-user
>
> >>> Betreff: Re: [scala-user] Implicit conversion and scalac
> >>
> >>> two
> >>>
> >>> On 28 Dec 2011, at 14:06, Edmondo Porcu wrote:
> >>>
> >>>> implicit def toRichCurrency(currency:Currency) =
> >>>> new {
> >>>> def hello = println("hello");
> >>>> def hi = println("hi");
> >>>> }
> >>>>
> >>>> def myMethod(currency:Currency){
> >>>> currency.hello
> >>>> currency.hi
> >>>> }
> >>>>
> >>>> One or two conversions?
> >>>>
> >>>> Best Regards
> >>>>
> >>>> 2011/12/28 Sciss
> >>>> if your pimp'ed Int's operators return an Int, you will see three
> >>> conversions.
> >>>>
> >>>> On 28 Dec 2011, at 14:00, Edmondo Porcu wrote:
> >>>>
> >>>>> Dear All,
> >>>>> how does the scala compiler handles implicit conversions?
> >>>>>
> >>>>> If in the same method I access 3 methods of PimpMyInt on the same
> Int
> >>> object, will the conversion be executed only once?
> >>>>>
> >>>>> Best Regards
> >>>>>
> >>>>> Edmondo
> >>>>
> >>>>
> >>>
> >
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Implicit conversion and scalac

unless it's the performance argument again ;) then they are bad

Am 28.12.2011 15:07, schrieb Dennis Haupt:
> structural types are ok for this
>
> -------- Original-Nachricht --------
>> Datum: Wed, 28 Dec 2011 14:46:47 +0100
>> Von: Sciss
>> An: Sciss
>> CC: "Dennis Haupt" , scala-user [at] googlegroups [dot] com, edmondo [dot] porcu [at] gmail [dot] com
>> Betreff: Re: [scala-user] Implicit conversion and scalac
>> ( p.s. although probably it is advisable to make a real pimped class with
>> methods 'hello' and 'hi', since with the code below you end up with
>> structural types, no? )
>>
>> On 28 Dec 2011, at 14:42, Sciss wrote:
>>
>>> but the VM may well be able to erase the penalty of instantiation
>> altogether, so in the spirit of the recent micro optimization discussions, i
>> would say, don't waste your time thinking about it _unless you encounter a real
>> performance problem related to it_.
>>> On 28 Dec 2011, at 14:16, Dennis Haupt wrote:
>>>
>>>> the compiler cannot reuse the enrichment instance because right now it
>> is not smart enough to see that the enrichment instance has no state.
>>>>
>>>>
>>>> -------- Original-Nachricht --------
>>>>> Datum: Wed, 28 Dec 2011 14:11:15 +0100
>>>>> Von: Sciss
>>>>> An: Edmondo Porcu , scala-user
>>
>>>>> Betreff: Re: [scala-user] Implicit conversion and scalac
>>>>> two
>>>>>
>>>>> On 28 Dec 2011, at 14:06, Edmondo Porcu wrote:
>>>>>
>>>>>> implicit def toRichCurrency(currency:Currency) =
>>>>>> new {
>>>>>> def hello = println("hello");
>>>>>> def hi = println("hi");
>>>>>> }
>>>>>>
>>>>>> def myMethod(currency:Currency){
>>>>>> currency.hello
>>>>>> currency.hi
>>>>>> }
>>>>>>
>>>>>> One or two conversions?
>>>>>>
>>>>>> Best Regards
>>>>>>
>>>>>> 2011/12/28 Sciss
>>>>>> if your pimp'ed Int's operators return an Int, you will see three
>>>>> conversions.
>>>>>> On 28 Dec 2011, at 14:00, Edmondo Porcu wrote:
>>>>>>
>>>>>>> Dear All,
>>>>>>> how does the scala compiler handles implicit conversions?
>>>>>>>
>>>>>>> If in the same method I access 3 methods of PimpMyInt on the same
>> Int
>>>>> object, will the conversion be executed only once?
>>>>>>> Best Regards
>>>>>>>
>>>>>>> Edmondo
>>>>>>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland