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

RE: Optional implicits in Scala?

2 replies
Jonathan Schwietert
Joined: 2011-02-02,
User offline. Last seen 42 years 45 weeks ago.

So quick question related to this (yes i'm reviving a thread from the
old mailing list - so i included the entire thread). Btw, this idea is
really awesome!

If I had the following methods & classes:

implicit def OptionalNumeric[T](implicit numeric: Numeric[T] =
null):Option[Numeric[T]] = Option(numeric)

def create[T](string: String)(implicit manifest: Manifest[T],
opNumeric: Option[Numeric[T]])
{
opNumeric match
{
case Some(numeric) => CreateSomeNumericWithString[T](string)
(numeric)
case None => throw new RuntimeException(…)
}
}

It seems like this is very close to the Context Bound pattern of ‘def
someMethod[T: SomeContextBound](param: String)’ but I the compiler
obviously doesn’t like:
def create[T: Option[Numeric]: Manifest](string: String)

So how would I use this syntax pattern for the above ‘create’ method?
It would look so clean!

Jonathan

From: √iktor Klang [mailto:viktor [dot] klang [at] gmail [dot] com]
Sent: Monday, December 06, 2010 6:02 AM
To: Josh Suereth
Cc: Chris Marshall; scala-internals [at] listes [dot] epfl [dot] ch; Jason Zaugg
Subject: Re: [scala-internals] Optional implicits in Scala?

On Mon, Dec 6, 2010 at 1:51 PM, Josh Suereth
wrote:
Btw- when searching for option[T] the compiler Will look in the
companion object for T. Combine this with the fact that default
parameters have lower precedence and you can do:
def foo[A](implicit value : Option[A] = None) = value
trait Foo
object Foo {
implicit def Makefoo = Some(new Foo {})
}
foo[Bar] // None
foo[Foo] // Some(Foo)
This could help remove the need for importing implicits to a
particular scope. Note that imported implicits should override what's
available from companion/package objects. So it's a pretty flexible
system

Yeah, it's powerful, we use it for automatic sender tracking in Akka

On Dec 6, 2010 5:35 AM, "√iktor Klang" wrote:

On Mon, Dec 6, 2010 at 11:20 AM, Chris Marshall
wrote:
Awesome!

Indeed it is. Beware tho, it's a tad harder to debug when you _think_
you have the implicit in scope and you really don't.

>
>
> > Date: Mon, 6 Dec 2010 10:45:59 +0100
> > Subject: Re: [scala-internals] Optional implicits ...

--
Viktor Klang,
Code Connoisseur
Work: Scalable Solutions
Code: github.com/viktorklang
Follow: twitter.com/viktorklang
Read: klangism.tumblr.com

--
Viktor Klang,
Code Connoisseur
Work: Scalable Solutions
Code: github.com/viktorklang
Follow: twitter.com/viktorklang
Read: klangism.tumblr.com

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Optional implicits in Scala?

On 2/2/11 9:31 AM, Jonathan Schwietert wrote:
> def create[T: Option[Numeric]: Manifest](string: String)
>
> So how would I use this syntax pattern for the above ‘create’ method?
> It would look so clean!

I predict there is a more clever answer available. But from the low
cleverness contingent: push the type parameter upward.

object Test {
class OptNumeric[T](val num: Option[Numeric[T]]) { }
object OptNumeric {
implicit def default[T](implicit num: Numeric[T] = null):
OptNumeric[T] =
new OptNumeric[T](Option(num))
}
def create[T: OptNumeric: Manifest](string: String): Unit = {
println(implicitly[OptNumeric[T]].num getOrElse "no numeric")
}

def main(args: Array[String]): Unit = {
create[Int]("5")
create[String]("5")
}
}
// output:
//
// scala.math.Numeric$IntIsIntegral$@4cb9e45a
// no numeric

Jonathan Schwietert
Joined: 2011-02-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Optional implicits in Scala?

Thanks Paul, i should have thought of that ;)

On Feb 2, 10:49 am, Paul Phillips wrote:
> On 2/2/11 9:31 AM, Jonathan Schwietert wrote:
>
> > def create[T: Option[Numeric]: Manifest](string: String)
>
> > So how would I use this syntax pattern for the above ‘create’ method?
> > It would look so clean!
>
> I predict there is a more clever answer available.  But from the low
> cleverness contingent: push the type parameter upward.
>
> object Test {
>    class OptNumeric[T](val num: Option[Numeric[T]]) { }
>    object OptNumeric {
>      implicit def default[T](implicit num: Numeric[T] = null):
> OptNumeric[T] =
>        new OptNumeric[T](Option(num))
>    }
>    def create[T: OptNumeric: Manifest](string: String): Unit = {
>       println(implicitly[OptNumeric[T]].num getOrElse "no numeric")
>    }
>
>    def main(args: Array[String]): Unit = {
>      create[Int]("5")
>      create[String]("5")
>    }}
>
> // output:
> //
> // scala.math.Numeric$IntIsIntegral$@4cb9e45a
> // no numeric

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