1

The following works:

class Outter {
    type Inner = Either[Int,String]
    type L = Left[Int,String]
    type R = Right[Int,String]

    def f(x: Inner) = 1
  }

  val o = new Outter
  o.f(new o.L(1))
  o.f(new o.R("name"))

but only because there is an explicit type member for all the sub-types of the Inner. Is it possible to construct a value from a sub-type of a path-dependent type without the need to mention them explicitly in the Outter ? Like:

class Outter {
    type Inner = Either[Int,String]
    def f(x: Inner) = 1
  }

  val o = new Outter
  o.f(new o.?!?(1))  // How do I express "that particular Left[Int,String] which is the sub-type of o.Inner
  o.f(new o.?!?("name")) // same as above here, but for Right

Related Path-dependent argument type is not enforced (?)

Community
  • 1
  • 1
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68

1 Answers1

2
type Inner = Either[Int, String]

This is a type alias. Inner is not a subclass of Either[Int, String] they are the same. Just syntactic sugar. So you can stil refer to the subclasses of Either[Int, String] as if they are subclasses of Inner

So the solution might be more straightforward then you imagined.

val o = new Outter
o.f(Left(1)) // How do I express "that particular Left[Int,String] which is the sub-type of o.Inner
o.f(Right("name")) // same as above here, but for Right
Pim Verkerk
  • 1,066
  • 7
  • 12
  • 1
    "This is a type alias. `Either[Int, String]` is not a subclass of `Either`" yeah I know. That was not my question (by the way, `Either[Int,String]` is *not* the same as`Either`. The former is a proper type, the later is a type constructor of arity 2). – Ashkan Kh. Nazary Feb 22 '16 at 14:50
  • The question is, how do I create `Left` and `Right` of the expected type (which is `o.Inner` and *not* `Inner`). I don't have a REPL handy but I think your code does not type check (type mismatch, required `o.Inner`, found `Left`) – Ashkan Kh. Nazary Feb 22 '16 at 14:51
  • "by the way, `Either[Int,String]` is not the same as `Either`". This is a typo on my part. I meant to say "`Inner` is not a subclass of `Either[Int, String]`". I edited my answer. – Pim Verkerk Feb 22 '16 at 14:56
  • 1
    Apparently you are right ! I'll experiment a lilbit and I'll get back – Ashkan Kh. Nazary Feb 22 '16 at 15:08
  • Kindly see the related question (I added it at the end of this one) – Ashkan Kh. Nazary Feb 22 '16 at 15:34