- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Optional implicit conversion.
Thu, 2012-01-19, 17:34
I am kind of confused among Sciss and Derek answer...which is the right one?
Best Regards
2012/1/19 Sciss <contact@sciss.de>
Best Regards
2012/1/19 Sciss <contact@sciss.de>
( commentary: to make sure that `exists` has priority over `default`, the trick is to put `exists` directly into the companion object, but have `default` in a trait that the companion object mixes in; that way it gets lower priority, and scalac has no conflict deciding between the two. )
On 19 Jan 2012, at 16:25, Sciss wrote:
> something around these lines
>
> trait NoConversion {
> implicit def default[ A, B ] : MaybeConversion[ A, B ] = MaybeConversion( None )
> }
> object MaybeConversion extends NoConversion {
> implicit def exists[ A, B ]( implicit view: A => B ) : MaybeConversion[ A, B ] =
> MaybeConversion( Some( view ))
> }
> final case class MaybeConversion[ A, B ]( option: Option[ A => B ])
>
> def test[ A ]( a: A )( implicit c: MaybeConversion[ A, Int ]) : Option[ Int ] =
> c.option.map( _( a ))
>
> test( "1" ) // None
> implicit def strToInt( s: String ) = s.toInt
> test( "2" ) // Some
>
> best, -sciss-
>
>
> On 19 Jan 2012, at 16:17, Edmondo Porcu wrote:
>
>> Dear all,
>>
>> Is it possible in scala to write a code as the following?
>>
>> if there is an available implicit conversion from A to B, transform A to B and put in the collection.
>>
>> else do nothing.
>>
>>
>> Thank you very much
>> Best Regards
>> Edmondo
>>
>>
>>
>>
>
Thu, 2012-01-19, 17:51
#2
Re: Optional implicit conversion.
Is there a preferred solution?
2012/1/19 √iktor Ҡlang <viktor.klang@gmail.com>
2012/1/19 √iktor Ҡlang <viktor.klang@gmail.com>
On Thu, Jan 19, 2012 at 5:41 PM, Derek Williams <derek@fyrie.net> wrote:
On Thu, Jan 19, 2012 at 9:34 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:I am kind of confused among Sciss and Derek answer...which is the right one?
Yep, we both did the same thing more or less. The main point is having a type class with one instance that requires an implicit A => B, and a fallback instance.
Or.... you just use a view-bound: http://stackoverflow.com/questions/4465948/what-are-scala-context-and-view-bounds
--
Derek Williams
--
Viktor Klang
Akka Tech LeadTypesafe - The software stack for applications that scale
Twitter: @viktorklang
Thu, 2012-01-19, 17:51
#3
Re: Optional implicit conversion.
2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>
Is there a preferred solution?
a view bound [A <% B] is just sugar for (implicit f: A => B), just like a context bound [A: B] is just sugar for (implicit x: B[A])
--
Derek Williams
Thu, 2012-01-19, 18:01
#4
Re: Optional implicit conversion.
they are the same, only derek add the part "...and put in the collection." which i left to you :)
On 19 Jan 2012, at 16:34, Edmondo Porcu wrote:
> I am kind of confused among Sciss and Derek answer...which is the right one?
>
> Best Regards
>
> 2012/1/19 Sciss
> ( commentary: to make sure that `exists` has priority over `default`, the trick is to put `exists` directly into the companion object, but have `default` in a trait that the companion object mixes in; that way it gets lower priority, and scalac has no conflict deciding between the two. )
>
> On 19 Jan 2012, at 16:25, Sciss wrote:
>
> > something around these lines
> >
> > trait NoConversion {
> > implicit def default[ A, B ] : MaybeConversion[ A, B ] = MaybeConversion( None )
> > }
> > object MaybeConversion extends NoConversion {
> > implicit def exists[ A, B ]( implicit view: A => B ) : MaybeConversion[ A, B ] =
> > MaybeConversion( Some( view ))
> > }
> > final case class MaybeConversion[ A, B ]( option: Option[ A => B ])
> >
> > def test[ A ]( a: A )( implicit c: MaybeConversion[ A, Int ]) : Option[ Int ] =
> > c.option.map( _( a ))
> >
> > test( "1" ) // None
> > implicit def strToInt( s: String ) = s.toInt
> > test( "2" ) // Some
> >
> > best, -sciss-
> >
> >
> > On 19 Jan 2012, at 16:17, Edmondo Porcu wrote:
> >
> >> Dear all,
> >>
> >> Is it possible in scala to write a code as the following?
> >>
> >> if there is an available implicit conversion from A to B, transform A to B and put in the collection.
> >>
> >> else do nothing.
> >>
> >>
> >> Thank you very much
> >> Best Regards
> >> Edmondo
> >>
> >>
> >>
> >>
> >
>
>
Thu, 2012-01-19, 18:01
#5
Re: Optional implicit conversion.
i think it's a matter of taste. so here is my taste: if you want concise code, i would use
(1) the view bounds when the method passes on the implicit to other methods
(2) use the long form if you actually need to refer to that conversion
because in (2) you would need to use implicitly[Conv] which kind of defeats the purpose of being more concise, plus adding another method call.
On 19 Jan 2012, at 16:49, Derek Williams wrote:
>
>
> 2012/1/19 Edmondo Porcu
> Is there a preferred solution?
>
>
> a view bound [A <% B] is just sugar for (implicit f: A => B), just like a context bound [A: B] is just sugar for (implicit x: B[A])
>
Thu, 2012-01-19, 18:11
#6
Re: Optional implicit conversion.
On Thu, Jan 19, 2012 at 9:34 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
I am kind of confused among Sciss and Derek answer...which is the right one?
Yep, we both did the same thing more or less. The main point is having a type class with one instance that requires an implicit A => B, and a fallback instance.
--
Derek Williams
Thu, 2012-01-19, 18:11
#7
Re: Optional implicit conversion.
On Thu, Jan 19, 2012 at 9:49 AM, Derek Williams <derek@fyrie.net> wrote:
2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>Is there a preferred solution?
a view bound [A <% B] is just sugar for (implicit f: A => B), just like a context bound [A: B] is just sugar for (implicit x: B[A])
err, maybe. Using A <% B will also pull in implicit defs that can do the conversion. Gah, I need a REPL to test to make sure it works before I give advice.
--
Derek Williams
Thu, 2012-01-19, 18:21
#8
Re: Optional implicit conversion.
i don't understand the question. 'view' is used as synonym for 'a function from A to B'. ("you can view an A as a B"). therefore, view-bound A <% B = implicit function argument from A to B.
case class X[ A, B ]( something: A )( implicit view: MaybeConversion[ A, B ])
'view' is not a keyword, you could call that anything else
case class X[ A, B ]( something: A )( implicit conv: MaybeConversion[ A, B ])
best, -sciss-
On 19 Jan 2012, at 17:04, Edmondo Porcu wrote:
> Just a last more question...how do you define an implicit view ?:)
>
> I have implemented sciss proposed solution, but I don't know how to pass my specific conversion to the constructor of the case class...
>
> Best
>
> 2012/1/19 Derek Williams
>
>
> On Thu, Jan 19, 2012 at 9:49 AM, Derek Williams wrote:
>
>
> 2012/1/19 Edmondo Porcu
> Is there a preferred solution?
>
>
> a view bound [A <% B] is just sugar for (implicit f: A => B), just like a context bound [A: B] is just sugar for (implicit x: B[A])
>
>
> err, maybe. Using A <% B will also pull in implicit defs that can do the conversion. Gah, I need a REPL to test to make sure it works before I give advice.
>
Thu, 2012-01-19, 18:31
#9
Re: Optional implicit conversion.
Just a last more question...how do you define an implicit view ?:)
I have implemented sciss proposed solution, but I don't know how to pass my specific conversion to the constructor of the case class...
Best
2012/1/19 Derek Williams <derek@fyrie.net>
I have implemented sciss proposed solution, but I don't know how to pass my specific conversion to the constructor of the case class...
Best
2012/1/19 Derek Williams <derek@fyrie.net>
On Thu, Jan 19, 2012 at 9:49 AM, Derek Williams <derek@fyrie.net> wrote:
2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>Is there a preferred solution?
a view bound [A <% B] is just sugar for (implicit f: A => B), just like a context bound [A: B] is just sugar for (implicit x: B[A])
err, maybe. Using A <% B will also pull in implicit defs that can do the conversion. Gah, I need a REPL to test to make sure it works before I give advice.
--
Derek Williams
Thu, 2012-01-19, 18:31
#10
Re: Optional implicit conversion.
how does the constructor of Converter look like?
you know you can use predefined function `Predef.identity` for `A => A` (`new Converter[ X, X ]( Some( identity ))`)
is Converter expecting the MaybeConversion? Then there is no conversion for an Option[ A => B ] defined, you would need to add that if you want it. Otherwise, let scalac do the conversion for you:
class Converter[ A, B ]( implicit fun: MaybeConversion[ A, B ]) {
def apply( a: A ) : Option[ B ] = fun.option.map( _.apply( a ))
}
implicit def allowIdentity[ A ] = MaybeConversion.exists( identity[ A ])
val x = new Converter[ Int, Int ]
x( 33 )
On 19 Jan 2012, at 17:17, Edmondo Porcu wrote:
> implicit def SelfConverter[X] = new Converter[X,X](Some(item=>item))
>
> my ide complains about: missing parameter type (item)
>
> :(
>
>
>
>
>
> 2012/1/19 Edmondo Porcu
> Just a last more question...how do you define an implicit view ?:)
>
> I have implemented sciss proposed solution, but I don't know how to pass my specific conversion to the constructor of the case class...
>
> Best
>
>
> 2012/1/19 Derek Williams
>
>
> On Thu, Jan 19, 2012 at 9:49 AM, Derek Williams wrote:
>
>
> 2012/1/19 Edmondo Porcu
> Is there a preferred solution?
>
>
> a view bound [A <% B] is just sugar for (implicit f: A => B), just like a context bound [A: B] is just sugar for (implicit x: B[A])
>
>
> err, maybe. Using A <% B will also pull in implicit defs that can do the conversion. Gah, I need a REPL to test to make sure it works before I give advice.
>
Thu, 2012-01-19, 18:41
#11
Re: Optional implicit conversion.
implicit def SelfConverter[X] = new Converter[X,X](Some(item=>item))
my ide complains about: missing parameter type (item)
:(
2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>
my ide complains about: missing parameter type (item)
:(
2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>
Just a last more question...how do you define an implicit view ?:)
I have implemented sciss proposed solution, but I don't know how to pass my specific conversion to the constructor of the case class...
Best
2012/1/19 Derek Williams <derek@fyrie.net>
On Thu, Jan 19, 2012 at 9:49 AM, Derek Williams <derek@fyrie.net> wrote:
2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>Is there a preferred solution?
a view bound [A <% B] is just sugar for (implicit f: A => B), just like a context bound [A: B] is just sugar for (implicit x: B[A])
err, maybe. Using A <% B will also pull in implicit defs that can do the conversion. Gah, I need a REPL to test to make sure it works before I give advice.
--
Derek Williams
Fri, 2012-01-20, 10:41
#12
Re: Optional implicit conversion.
Last questions for you masters... :)
Does a identity conversion gets optimized away ? :)
Best Regards
2012/1/19 Sciss <contact@sciss.de>
Does a identity conversion gets optimized away ? :)
Best Regards
2012/1/19 Sciss <contact@sciss.de>
how does the constructor of Converter look like?
you know you can use predefined function `Predef.identity` for `A => A` (`new Converter[ X, X ]( Some( identity ))`)
is Converter expecting the MaybeConversion? Then there is no conversion for an Option[ A => B ] defined, you would need to add that if you want it. Otherwise, let scalac do the conversion for you:
class Converter[ A, B ]( implicit fun: MaybeConversion[ A, B ]) {
def apply( a: A ) : Option[ B ] = fun.option.map( _.apply( a ))
}
implicit def allowIdentity[ A ] = MaybeConversion.exists( identity[ A ])
val x = new Converter[ Int, Int ]
x( 33 )
On 19 Jan 2012, at 17:17, Edmondo Porcu wrote:
> implicit def SelfConverter[X] = new Converter[X,X](Some(item=>item))
>
> my ide complains about: missing parameter type (item)
>
> :(
>
>
>
>
>
> 2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>
> Just a last more question...how do you define an implicit view ?:)
>
> I have implemented sciss proposed solution, but I don't know how to pass my specific conversion to the constructor of the case class...
>
> Best
>
>
> 2012/1/19 Derek Williams <derek@fyrie.net>
>
>
> On Thu, Jan 19, 2012 at 9:49 AM, Derek Williams <derek@fyrie.net> wrote:
>
>
> 2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>
> Is there a preferred solution?
>
>
> a view bound [A <% B] is just sugar for (implicit f: A => B), just like a context bound [A: B] is just sugar for (implicit x: B[A])
>
>
> err, maybe. Using A <% B will also pull in implicit defs that can do the conversion. Gah, I need a REPL to test to make sure it works before I give advice.
>
> --
> Derek Williams
>
>
>
Fri, 2012-01-20, 11:21
#13
Re: Optional implicit conversion.
maybe. just try.
-------- Original-Nachricht --------
> Datum: Fri, 20 Jan 2012 10:34:39 +0100
> Von: Edmondo Porcu
> An: Sciss
> CC: Derek Williams , "√iktor Ҡlang" , scala-user
> Betreff: Re: [scala-user] Optional implicit conversion.
> Last questions for you masters... :)
>
> Does a identity conversion gets optimized away ? :)
>
> Best Regards
>
> 2012/1/19 Sciss
>
> > how does the constructor of Converter look like?
> >
> > you know you can use predefined function `Predef.identity` for `A => A`
> > (`new Converter[ X, X ]( Some( identity ))`)
> >
> > is Converter expecting the MaybeConversion? Then there is no conversion
> > for an Option[ A => B ] defined, you would need to add that if you want
> it.
> > Otherwise, let scalac do the conversion for you:
> >
> > class Converter[ A, B ]( implicit fun: MaybeConversion[ A, B ]) {
> > def apply( a: A ) : Option[ B ] = fun.option.map( _.apply( a ))
> > }
> >
> > implicit def allowIdentity[ A ] = MaybeConversion.exists( identity[ A ])
> >
> > val x = new Converter[ Int, Int ]
> > x( 33 )
> >
> >
> >
> > On 19 Jan 2012, at 17:17, Edmondo Porcu wrote:
> >
> > > implicit def SelfConverter[X] = new Converter[X,X](Some(item=>item))
> > >
> > > my ide complains about: missing parameter type (item)
> > >
> > > :(
> > >
> > >
> > >
> > >
> > >
> > > 2012/1/19 Edmondo Porcu
> > > Just a last more question...how do you define an implicit view ?:)
> > >
> > > I have implemented sciss proposed solution, but I don't know how to
> pass
> > my specific conversion to the constructor of the case class...
> > >
> > > Best
> > >
> > >
> > > 2012/1/19 Derek Williams
> > >
> > >
> > > On Thu, Jan 19, 2012 at 9:49 AM, Derek Williams
> wrote:
> > >
> > >
> > > 2012/1/19 Edmondo Porcu
> > > Is there a preferred solution?
> > >
> > >
> > > a view bound [A <% B] is just sugar for (implicit f: A => B), just
> like
> > a context bound [A: B] is just sugar for (implicit x: B[A])
> > >
> > >
> > > err, maybe. Using A <% B will also pull in implicit defs that can do
> the
> > conversion. Gah, I need a REPL to test to make sure it works before I
> give
> > advice.
> > >
> > > --
> > > Derek Williams
> > >
> > >
> > >
> >
> >
Thu, 2012-02-09, 16:11
#14
Re: Optional implicit conversion.
Dear all,I have implemented this according to your suggestions.
However, I have now the following problem.
If from class A,
I do call a method tryConversion[T](b.something) that works fine
if I do call directly from b, tryConversion[T](something) that doesn't work, the implicit conversion is not found.
I have checked the imports and they are the same. How can I debug this? Thank you for your help
Best Regards
Edmondo
2012/1/20 Dennis Haupt <h-star@gmx.de>
However, I have now the following problem.
If from class A,
I do call a method tryConversion[T](b.something) that works fine
if I do call directly from b, tryConversion[T](something) that doesn't work, the implicit conversion is not found.
I have checked the imports and they are the same. How can I debug this? Thank you for your help
Best Regards
Edmondo
2012/1/20 Dennis Haupt <h-star@gmx.de>
maybe. just try.
-------- Original-Nachricht --------
> Datum: Fri, 20 Jan 2012 10:34:39 +0100
> Von: Edmondo Porcu <edmondo.porcu@gmail.com>
> An: Sciss <contact@sciss.de>
> CC: Derek Williams <derek@fyrie.net>, "√iktor Ҡlang" <viktor.klang@gmail.com>, scala-user <scala-user@googlegroups.com>
> Betreff: Re: [scala-user] Optional implicit conversion.
> Last questions for you masters... :)
>
> Does a identity conversion gets optimized away ? :)
>
> Best Regards
>
> 2012/1/19 Sciss <contact@sciss.de>
>
> > how does the constructor of Converter look like?
> >
> > you know you can use predefined function `Predef.identity` for `A => A`
> > (`new Converter[ X, X ]( Some( identity ))`)
> >
> > is Converter expecting the MaybeConversion? Then there is no conversion
> > for an Option[ A => B ] defined, you would need to add that if you want
> it.
> > Otherwise, let scalac do the conversion for you:
> >
> > class Converter[ A, B ]( implicit fun: MaybeConversion[ A, B ]) {
> > def apply( a: A ) : Option[ B ] = fun.option.map( _.apply( a ))
> > }
> >
> > implicit def allowIdentity[ A ] = MaybeConversion.exists( identity[ A ])
> >
> > val x = new Converter[ Int, Int ]
> > x( 33 )
> >
> >
> >
> > On 19 Jan 2012, at 17:17, Edmondo Porcu wrote:
> >
> > > implicit def SelfConverter[X] = new Converter[X,X](Some(item=>item))
> > >
> > > my ide complains about: missing parameter type (item)
> > >
> > > :(
> > >
> > >
> > >
> > >
> > >
> > > 2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>
> > > Just a last more question...how do you define an implicit view ?:)
> > >
> > > I have implemented sciss proposed solution, but I don't know how to
> pass
> > my specific conversion to the constructor of the case class...
> > >
> > > Best
> > >
> > >
> > > 2012/1/19 Derek Williams <derek@fyrie.net>
> > >
> > >
> > > On Thu, Jan 19, 2012 at 9:49 AM, Derek Williams <derek@fyrie.net>
> wrote:
> > >
> > >
> > > 2012/1/19 Edmondo Porcu <edmondo.porcu@gmail.com>
> > > Is there a preferred solution?
> > >
> > >
> > > a view bound [A <% B] is just sugar for (implicit f: A => B), just
> like
> > a context bound [A: B] is just sugar for (implicit x: B[A])
> > >
> > >
> > > err, maybe. Using A <% B will also pull in implicit defs that can do
> the
> > conversion. Gah, I need a REPL to test to make sure it works before I
> give
> > advice.
> > >
> > > --
> > > Derek Williams
> > >
> > >
> > >
> >
> >
Thu, 2012-02-09, 16:31
#15
Re: Optional implicit conversion.
you cannot "debug" it.
i've written down all the rules here:
http://rapidshare.com/files/3045603786/scc.pdf
also, try intellij idea + scala plugin + hotkey ctrl+shift+q, it helps a lot
-------- Original-Nachricht --------
> Datum: Thu, 9 Feb 2012 16:03:56 +0100
> Von: Edmondo Porcu
> An: Dennis Haupt
> CC: contact@sciss.de, scala-user@googlegroups.com, viktor.klang@gmail.com, derek@fyrie.net
> Betreff: Re: [scala-user] Optional implicit conversion.
> Dear all,
> I have implemented this according to your suggestions.
>
> However, I have now the following problem.
>
> If from class A,
>
> I do call a method tryConversion[T](b.something) that works fine
>
> if I do call directly from b, tryConversion[T](something) that doesn't
> work, the implicit conversion is not found.
>
> I have checked the imports and they are the same.
> How can I debug this? Thank you for your help
>
> Best Regards
>
> Edmondo
>
> 2012/1/20 Dennis Haupt
>
> > maybe. just try.
> >
> > -------- Original-Nachricht --------
> > > Datum: Fri, 20 Jan 2012 10:34:39 +0100
> > > Von: Edmondo Porcu
> > > An: Sciss
> > > CC: Derek Williams , "√iktor Ҡlang" <
> > viktor.klang@gmail.com>, scala-user
> > > Betreff: Re: [scala-user] Optional implicit conversion.
> >
> > > Last questions for you masters... :)
> > >
> > > Does a identity conversion gets optimized away ? :)
> > >
> > > Best Regards
> > >
> > > 2012/1/19 Sciss
> > >
> > > > how does the constructor of Converter look like?
> > > >
> > > > you know you can use predefined function `Predef.identity` for `A =>
> A`
> > > > (`new Converter[ X, X ]( Some( identity ))`)
> > > >
> > > > is Converter expecting the MaybeConversion? Then there is no
> conversion
> > > > for an Option[ A => B ] defined, you would need to add that if you
> want
> > > it.
> > > > Otherwise, let scalac do the conversion for you:
> > > >
> > > > class Converter[ A, B ]( implicit fun: MaybeConversion[ A, B ]) {
> > > > def apply( a: A ) : Option[ B ] = fun.option.map( _.apply( a ))
> > > > }
> > > >
> > > > implicit def allowIdentity[ A ] = MaybeConversion.exists( identity[
> A
> > ])
> > > >
> > > > val x = new Converter[ Int, Int ]
> > > > x( 33 )
> > > >
> > > >
> > > >
> > > > On 19 Jan 2012, at 17:17, Edmondo Porcu wrote:
> > > >
> > > > > implicit def SelfConverter[X] = new
> Converter[X,X](Some(item=>item))
> > > > >
> > > > > my ide complains about: missing parameter type (item)
> > > > >
> > > > > :(
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > 2012/1/19 Edmondo Porcu
> > > > > Just a last more question...how do you define an implicit view ?:)
> > > > >
> > > > > I have implemented sciss proposed solution, but I don't know how
> to
> > > pass
> > > > my specific conversion to the constructor of the case class...
> > > > >
> > > > > Best
> > > > >
> > > > >
> > > > > 2012/1/19 Derek Williams
> > > > >
> > > > >
> > > > > On Thu, Jan 19, 2012 at 9:49 AM, Derek Williams
> > > wrote:
> > > > >
> > > > >
> > > > > 2012/1/19 Edmondo Porcu
> > > > > Is there a preferred solution?
> > > > >
> > > > >
> > > > > a view bound [A <% B] is just sugar for (implicit f: A => B), just
> > > like
> > > > a context bound [A: B] is just sugar for (implicit x: B[A])
> > > > >
> > > > >
> > > > > err, maybe. Using A <% B will also pull in implicit defs that can
> do
> > > the
> > > > conversion. Gah, I need a REPL to test to make sure it works before
> I
> > > give
> > > > advice.
> > > > >
> > > > > --
> > > > > Derek Williams
> > > > >
> > > > >
> > > > >
> > > >
> > > >
> >
On Thu, Jan 19, 2012 at 5:41 PM, Derek Williams <derek@fyrie.net> wrote:
Or.... you just use a view-bound: http://stackoverflow.com/questions/4465948/what-are-scala-context-and-view-bounds
--
Viktor Klang
Akka Tech LeadTypesafe - The software stack for applications that scale
Twitter: @viktorklang