Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Values of abstract member types

I'm trying to use this kind of code:

trait Outer {
  type Inner

  def f(x:Inner) : Void 
}

object test {
  def apply(o: Outer, i : Outer#Inner) : Void = 
    o.f(i)
}

I got an error in the last but one line:

type mismatch; found : i.type (with underlying type Outer#Inner) required: o.Inner

If I change apply's signature to

def apply(o: Outer, i : o.Inner) : Void

then I got an error:

illegal dependent method type

Is it possible to get this piece of code work?

like image 286
Artem Pelenitsyn Avatar asked Dec 06 '25 14:12

Artem Pelenitsyn


1 Answers

You can take advantage of method dependent types (see What are some compelling use cases for dependent method types? by example). This will require you to use 2 parameter lists:

trait Outer {
  type Inner
  def f(x:Inner): Unit 
}

object test {
  def apply( o: Outer )( i : o.Inner) { o.f(i) }
}
like image 79
Régis Jean-Gilles Avatar answered Dec 08 '25 10:12

Régis Jean-Gilles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!