Upper bound for a type parameter and underscore

75 views
Skip to first unread message

Rado Buranský

unread,
Jan 12, 2015, 4:33:14 AM1/12/15
to scala-l...@googlegroups.com
Can anybody point me to Scala language specification document where meaning of underscore as a type parameter is defined?

The following code doesn't compile. I don't have a real issue, so you don't need to tell me how to avoid it. I would expect that if there is an upper bound defined, then it is taken into account in this case.

trait Upper {
 
def up: Int
}

trait Underscore[T <: Upper] {
 
def t: T
}

object Underscore {
 
// This doesn't compile, beacuse "value up is not a member of _$1", even thoug there is the upper bound.
  def f(u: Underscore[_]) = u.t.up
}

Inspired by this question: http://stackoverflow.com/questions/27890745/f-bounded-polymorphic-return-types-in-scala/27891422

Thanks

Toby

unread,
Jan 12, 2015, 4:47:26 AM1/12/15
to scala-l...@googlegroups.com
Hi Rado,

I am the OP on StackOverflow ;) I actually found this (Emphasis is mine):


Section 3.2.10 > Placeholder Syntax for Existential Types

Scala supports a placeholder syntax for existential types. A wildcard type is of the form _>:L<:U. Both bound clauses may be omitted. If a lower bound clause >:L is missing, >:scala.Nothing is assumed. If an upper bound clause <:U is missing, <:scala.Any is assumed. 

Thus the fact that the [_] ignores any upper bound defined in the type definition is stated, though in my view this is still illogical because it requires duplication of Upper Bound information wherever the class is used, even if no new information is added.

Hanns Holger Rutz

unread,
Jan 12, 2015, 4:59:57 AM1/12/15
to scala-l...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/12/2015 10:47 AM, Toby wrote:
> Thus the fact that the [_] ignores any upper bound defined in the
> type definition is stated, though in my view this is still
> illogical because it requires duplication of Upper Bound
> information wherever the class is used, even if no new information
> is added.

This is in fact extremely annoying when working with F-bounded types
as you'll have to restate the bound each and everywhere.

It would be great if one could use a type member instead, but I have
never managed to get this to work as seamlessly as the type
constructor parameter.

https://stackoverflow.com/questions/23481991/attempting-to-model-f-bounded-polymorphism-as-a-type-member-in-scala?rq=1

trait Sys { me =>
import me.{S => S1}
type S <: Sys { type S = S1 } // ???

type Tx <: Txn[S]

type Vr[A] <: Var[S#Tx, A]
}

trait Txn[S <: Sys] {
def newVar[A]: S#Vr[A]
}

trait Var[Tx, A] {
def apply()(implicit tx: Tx): A
def update(value: A)(implicit tx: Tx): Unit
}

trait Foo[S <: Sys] {
def test(implicit tx: S#Tx): Unit = {
val v = tx.newVar[Int]
v() = 33 // FAIL
assert(v() == 33) // FAIL
}
}



best, .h.h.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBAgAGBQJUs5sWAAoJEKZFmaPaYk6QYsQP/2gt4BJprsd5CRbHK6b0HltY
67yuCL/HWs7oGv5exsIB5Pkd/0Bi8piF3WlwKHQtO0MvRkZoU2fvlKCAdam/CSUK
VTznIXoVrJpSu1QoGpJQo6PCeTVK8sI0CLFceHRnuUc+SsEX7ZskpyJkKyvFU4Me
bYSUky1bJ8GlTaALDqcO80fBoVNimDXo8+9tmhaSde3srQPBZ34Gmb71Nvl1yVjb
fAnp8V4X4iaU2ngn6wGTwULOBQ3hKqHjkA4a6y+qINxwiEAXmi3A9w6jj2CO8Pdy
xIldahKPbdX1Zx+BNwF9kJ+jV9Yj4DhKpOwywUvh25jQRaIWj4twoSyKefWcMg2g
JJ2mFMoK/Tjh2UZ0/U/izlVa2geoy0crTx1nWvd9KEZioW60XOGnQhggnvCR7w06
s/uwcEDtSfTM0wGPXxJ/V6xJQLrq3Eeo8vnSeE9iZtiRmP7KoEPKp+VokiftZD0R
jByNiwKg/SYgmOYGVV4qDAdvaArDD/ZV0i4Jr8+buaf4NHlkYvQtq5k4sgkTkIb7
HAPnOtw24r3zB27Qa3uKQYhKbSEa811l6Z0YQLvtGla4Gx1dCoC+6F7GmicjF2ZL
Mbdx8SGhMLU88aSsuIPt4xFL/csq59r11IYimHAQGD0R5VcdsOGWMCB8y/JZWtQ8
OJgh3VfOccIrH7GvHTKr
=KV/C
-----END PGP SIGNATURE-----
Reply all
Reply to author
Forward
0 new messages