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

lower and upper bound

2 replies
ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.

I'd like to see an example of specifying both a lower and an upper bound
in a type parameter. I tried for example this:

def m[Int >: T <: AnyRef](x: T) = println(x)

but I get:

:4: error: not found: type T

If I omit the lower bound, it works. I got the syntax from Programming
in Scala, perhaps it's a typo there? In 7.3 it reads: "One can also
combine lower and upper bounds, as in T >: S <: U."

Thanks,
Dimitris Andreou

Tony Sloane
Joined: 2009-01-07,
User offline. Last seen 2 years 32 weeks ago.
Re: lower and upper bound

On 07/01/2009, at 4:35 PM, Andreou Dimitris wrote:

> I'd like to see an example of specifying both a lower and an upper
> bound in a type parameter. I tried for example this:
>
> def m[Int >: T <: AnyRef](x: T) = println(x)
>
> but I get:
>
> :4: error: not found: type T

Try

def m[T >: Int <: AnyRef](x: T) = println(x)

>
>
> If I omit the lower bound, it works. I got the syntax from
> Programming in Scala, perhaps it's a typo there? In 7.3 it reads:
> "One can also combine lower and upper bounds, as in T >: S <: U."

In this excerpt, T is the type whose bounds you are defining, S is the
lower bound and U is the upper bound. T must come first, not in the
middle.

regards,
Tony

loverdos
Joined: 2008-11-18,
User offline. Last seen 2 years 27 weeks ago.
Re: lower and upper bound
Right, it has to go at the beginning. I was wondering, though, whether we could introduce in Scala this special syntax: 
 S <: T <: U
since it is mathematically/aesthetically more appealing.
Christos.

On Wed, Jan 7, 2009 at 7:49 AM, Tony Sloane <inkytonik@gmail.com> wrote:
On 07/01/2009, at 4:35 PM, Andreou Dimitris wrote:

I'd like to see an example of specifying both a lower and an upper bound in a type parameter. I tried for example this:

def m[Int >: T <: AnyRef](x: T) = println(x)

but I get:

<console>:4: error: not found: type T

Try

  def m[T >: Int <: AnyRef](x: T) = println(x)



If I omit the lower bound, it works. I got the syntax from Programming in Scala, perhaps it's a typo there? In 7.3 it reads: "One can also combine lower and upper bounds, as in T >: S <: U."

In this excerpt, T is the type whose bounds you are defining, S is the lower bound and U is the upper bound.  T must come first, not in the middle.

regards,
Tony




--
 __~O
-\ <,       Christos KK Loverdos
(*)/ (*)      http://ckkloverdos.com

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