5

Is there any way, to get to the type of A from Some[A]?

type X = Some[Int]
type Y = ??? // what do I have to write here to get `Int`

I can define my own Option-type that allows this:

sealed trait Option[+A]
case object None extends Option[Nothing]
case class Some[+A](a: A) {
  type Inner = A
}

and then use

type X = Some[Int]
type Y = X#Inner

Is this also possible somehow with the normal Scala Option type?

Fabian Schmitthenner
  • 1,696
  • 10
  • 22
  • It reminds me of https://stackoverflow.com/questions/29038214/why-scala-does-not-have-a-decltype and https://stackoverflow.com/questions/29034921/can-function-type-be-defined-by-inference – Suma Apr 05 '17 at 08:51
  • 1
    I think in general, this might not be possible, but depending on your use case you could obtain it through something like `def x[T](opt: Option[T]) {/* T is available here*/}` or maybe with path-dependent type. – Mateusz Kubuszok Apr 05 '17 at 09:03

2 Answers2

2

here is a solution that uses path dependent type to recover type from value:

  trait IsOption[F]{
    type T    
    def apply(f: F): Option[T]
  }  
  object IsOption{
    def apply[F](implicit isf: IsOption[F]) = isf    
    implicit def mk[A] = new IsOption[Option[A]]{
      type T = A      
      def apply(f: Option[A]): Option[A] = f
    }
  }  
  def getInner[A](in:A)(implicit inner: IsOption[A]): Option[inner.T] = inner(in)

The answer is heavily inspired from this slide of a brilliant presentation: http://wheaties.github.io/Presentations/Scala-Dep-Types/dependent-types.html#/2/1

You have a function that receives an opaque A, but you recover the fact that it is an option and the inner type via the IsOption[A] implicit.

I appreciate that this is not exactly what you asked for, but when you use such type-dependent types. you need to have a concrete value from which you recover a type.

V-Lamp
  • 1,630
  • 10
  • 18
  • This solution does not need to have an `A` value to get the type `T`, only an implicit `IsOption[A]` (which will always be in scope when needed). So this is what was asked for, IMO. – Cyrille Corpet Apr 05 '17 at 09:48
  • However, you cannot do something like `type Inner = IsOption[Option[Int]]#T` But yes, a function like `def getInner[A](implicit inner: IsOption[A]): Option[inner.T] = ???` is valid but possibly of limited value. – V-Lamp Apr 05 '17 at 09:51
  • 2
    You can do `val isOpt = implicitly[Option[T]]; type Inner = isOpt.T` – Cyrille Corpet Apr 05 '17 at 09:54
1

You can write a simple type function as follows:

scala> type X = Some[Int]
defined type alias X

scala> type F[H <: Option[A], A] = A
defined type alias F

scala> type Y = F[X, Int]
defined type alias Y

scala> implicitly[Y =:= Int]
res3: =:=[Y,Int] = <function1>

Without partial type parameter application / inference it's not very usefull, but it works...

OlivierBlanvillain
  • 7,701
  • 4
  • 32
  • 51