Structural types and parameter bounds

33 views
Skip to first unread message

Mikhail Vorozhtsov

unread,
Mar 22, 2011, 7:45:15 AM3/22/11
to scala...@googlegroups.com
Hi, list.

I'm getting puzzled here. Why doesn't the following code typecheck?

$ cat O.scala 

trait In {
  type Out
}

def f[O, I <: In { type Out = O }](i: I) {}

def g[I <: In](i: I) = f[I#Out, I](i: I)

$ scala O.scala
/home/mikhail.vorozhtsov/O.scala:8: error: type arguments [I#Out,I] do not conform to method f's type parameter bounds [O,I <: this.In{type Out = O}]
def g[I <: In](i: I) = f[I#Out, I](i: I)
                        ^
one error found
especially when
def f[O, I <: In](i: I)(implicit w: I#Out =:= O) {}
does.
 

Ruediger Keller

unread,
Mar 22, 2011, 9:26:22 AM3/22/11
to scala...@googlegroups.com
Why do you try to specify the type parameter O separately for method
f? It is already available as I#Out, just as you do it for g.

You can do:

def f[I <: In](i: I) {}

Or something like:

def f[I <: In](i: I)(implicit ev: I#Out =:= Int) { }

Not sure what you try to achieve here.

Regards,
Rüdiger


2011/3/22 Mikhail Vorozhtsov <mikhail.v...@gmail.com>:

Mikhail Vorozhtsov

unread,
Mar 22, 2011, 12:56:42 PM3/22/11
to scala...@googlegroups.com, Ruediger Keller
It's a reduced test case. =:= has very limited usage since it's not transitive or
even symmetric and doesn't aid inference the way that parameter bounds do.

Here is a more motivating example:

trait Cap

trait Op {
  type C <: Cap
}

trait Ops { list =>
  type C <: Cap
  def seq: Seq[Op { type C >: list.C <: Cap }]
}

final class Ops1[O <: Op](op: O) { list =>
  type C = O#C
  def seq: Seq[Op { type C >: list.C <: Cap }] = Seq[O](op)
}

error: type mismatch;
 found   : Seq[O]
 required: Seq[this.Op{type C >: Ops1.this.C <: this.Cap}]

Meaning that O is not a subtype of Op { type C >: O#C <: Cap }, which I find very disturbing.
And indeed:

scala> def f[O <: Op] = implicitly[O <:< Op { type C = O#C }]
<console>:9: error: could not find implicit value for parameter e: <:<[O,Op{type C = O#C}]
Reply all
Reply to author
Forward
0 new messages