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

Evidence parameters

4 replies
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.

I stumbled across the following quite by chance while try to track
down an AST node position bug. It looks like an interesting new
feature ... has it been discussed anywhere?

miles@lewis:scala$ dists/latest/bin/scala
Welcome to Scala version 2.8.0.r18324-b20090930195017 (Java
HotSpot(TM) Server VM, Java 1.6.0_14).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class A[T]
defined class A

scala> class B[T : A]
defined class B

scala> implicit val as = new A[String]
as: A[String] = A@2250d5

scala> new B
res0: B[String] = B@15a4a77

Cheers,

Miles

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Evidence parameters

On Wed, Sep 30, 2009 at 8:31 PM, Miles Sabin wrote:
> scala> class A[T]
> defined class A
>
> scala> class B[T : A]
> defined class B
>
> scala> implicit val as = new A[String]
> as: A[String] = A@2250d5
>
> scala> new B
> res0: B[String] = B@15a4a77

Am I right that

class A[T : B]

is sugar for,

class A[T](implicit evidence : B[T])

?

Cheers,

Miles

David Hall 4
Joined: 2009-08-21,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Evidence parameters

On Wed, Sep 30, 2009 at 2:34 PM, Miles Sabin wrote:
> On Wed, Sep 30, 2009 at 8:31 PM, Miles Sabin wrote:
>> scala> class A[T]
>> defined class A
>>
>> scala> class B[T : A]
>> defined class B
>>
>> scala> implicit val as = new A[String]
>> as: A[String] = A@2250d5
>>
>> scala> new B
>> res0: B[String] = B@15a4a77
>
> Am I right that
>
>  class A[T : B]
>
> is sugar for,
>
>  class A[T](implicit evidence : B[T])

I believe so. That's how I've used it.

You can then recover B[T] with:

implicitly[B[T]]

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Evidence parameters
It's mentioned on the array SID. It's called a Context Bound.

On Wed, Sep 30, 2009 at 4:31 PM, Miles Sabin <miles@milessabin.com> wrote:
I stumbled across the following quite by chance while try to track
down an AST node position bug. It looks like an interesting new
feature ... has it been discussed anywhere?

miles@lewis:scala$ dists/latest/bin/scala
Welcome to Scala version 2.8.0.r18324-b20090930195017 (Java
HotSpot(TM) Server VM, Java 1.6.0_14).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class A[T]
defined class A

scala> class B[T : A]
defined class B

scala> implicit val as = new A[String]
as: A[String] = A@2250d5

scala> new B
res0: B[String] = B@15a4a77

Cheers,


Miles

--
Miles Sabin
tel: +44 (0)7813 944 528
skype:  milessabin
http://www.chuusai.com/
http://twitter.com/milessabin



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Evidence parameters

On Thu, Oct 1, 2009 at 2:55 PM, Daniel Sobral wrote:
> It's mentioned on the array SID. It's called a Context Bound.

Yup, I saw that ... thanks.

Cheers,

Miles

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