existential-type

Why does scala require existential types to restrict a generic bound?

旧城冷巷雨未停 提交于 2020-06-18 15:54:02
问题 With the following class hierarchy: trait Provider[A] { def get(): Seq[A] } abstract class ProviderImpl[A] extends Provider[A] { final override def get(): Seq[A] = Seq() } trait HasX { def getX: Int } trait RefinedProvider[A <: HasX] extends Provider[A] class TypedProviderImpl extends ProviderImpl[HasX] with RefinedProvider[HasX] I want to be able to do this: val provider: RefinedProvider[_] = new TypedProviderImpl() provider.get() map (_.getX) But it doesn't work, because the return type of

How to pass extential type of tuple to invariant generic function?

青春壹個敷衍的年華 提交于 2020-05-17 06:13:07
问题 I have an extential type of tuple, and I want to pass it to a generic function (I use ClassTag in this example but it is a custom type class with invariant type parameter): type TC = (ClassTag[T], Option[T]) forSome {type T} def foo[T](ct: ClassTag[T], o: Option[T]) = {} val tc: TC = (classTag[String], Option[String](null)) foo(tc._1, tc._2) This gives me error: error: type mismatch; found : scala.reflect.ClassTag[T] required: scala.reflect.ClassTag[Any] Note: T <: Any, but trait ClassTag is

Existential Quantification over Values

百般思念 提交于 2020-05-14 16:25:30
问题 I came across existential quantification over values in the Scala Language Specification (3.2.10 Existential Types). x: y.Inner forSome{val y : Outer} Does someone have illustrative use cases for it? T forSome {val x: S} is defined as T forSome { type t <: S with Singleton } . The Singletron trait is mentioned in the Specification (3.2.1 Singleton Types) but I could not find it in the Scaladoc. Where is it defined? 回答1: It is useful along with inner classes as alluded in the type names. See

Existential Quantification over Values

冷暖自知 提交于 2020-05-14 16:25:28
问题 I came across existential quantification over values in the Scala Language Specification (3.2.10 Existential Types). x: y.Inner forSome{val y : Outer} Does someone have illustrative use cases for it? T forSome {val x: S} is defined as T forSome { type t <: S with Singleton } . The Singletron trait is mentioned in the Specification (3.2.1 Singleton Types) but I could not find it in the Scaladoc. Where is it defined? 回答1: It is useful along with inner classes as alluded in the type names. See

Trait runtime type of type parameter through TypeTag when used with Existential type in Scala

送分小仙女□ 提交于 2020-03-20 12:01:07
问题 I have trait with type parameter. To get the runtime type I use TypeTag . However, when this trait (and its classes) are used with existential type in a Collection, e.g. List or Map , TypeTag is "lost". Here is an example of standard way to use Type Tag: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ scala> trait Animal[T] { | def typeT()(implicit t: TypeTag[T]) = t.tpe | } defined trait Animal scala> scala> class Dog extends Animal[Int] defined class

Trait runtime type of type parameter through TypeTag when used with Existential type in Scala

十年热恋 提交于 2020-03-20 11:57:19
问题 I have trait with type parameter. To get the runtime type I use TypeTag . However, when this trait (and its classes) are used with existential type in a Collection, e.g. List or Map , TypeTag is "lost". Here is an example of standard way to use Type Tag: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ scala> trait Animal[T] { | def typeT()(implicit t: TypeTag[T]) = t.tpe | } defined trait Animal scala> scala> class Dog extends Animal[Int] defined class

Can all usages of `forSome` be replaced by an equivalent usage of `_`?

亡梦爱人 提交于 2020-01-12 04:35:11
问题 For instance, List[T] forSome { type T } is equivalent to List[_] , but is this true for every possible usage of forSome or are there cases where forSome cannot be replaced by an equivalent of the second syntax? 回答1: No, not all usages can be thus converted. Something like this (thanks to retronym , below, who should be getting the upvotes on this one) def foo(xs: Map[T, T] forSome { type T}) The point here is that I can use the existential in more than one place but it is the same

Skolemization of existentially typed expressions

风格不统一 提交于 2020-01-12 03:31:50
问题 In Scala, the following expression raises a type error: val pair: (A => String, A) forSome { type A } = ( { a: Int => a.toString }, 19 ) pair._1(pair._2) As mentioned in SI-9899 and this answer, this is correct according to the spec: I think this is working as designed as per SLS 6.1: "The following skolemization rule is applied universally for every expression: If the type of an expression would be an existential type T, then the type of the expression is assumed instead to be a

Juggling existentials without unsafeCoerce

别说谁变了你拦得住时间么 提交于 2020-01-01 04:47:08
问题 Lately I have been playing with this type, which I understand to be an encoding of the free distributive functor (for tangential background on that, see this answer): data Ev g a where Ev :: ((g x -> x) -> a) -> Ev g a deriving instance Functor (Ev g) The existential constructor ensures I can only consume an Ev g by supplying a polymorphic extractor forall x. g x -> x , and that the lift and lower functions of the free construction can be given compatible types: runEv :: Ev g a -> (forall x.

Can an existentially quantified type variable be forced to have only a single type?

╄→гoц情女王★ 提交于 2019-12-30 10:12:39
问题 Consider the following code trait Foo[T] { def one: Foo[_ >: T] def two: T def three(x: T) } def test[T](f: Foo[T]) = { val b = f.one b.three(b.two) } The method test fails to type check. It says: found : (some other)_$1(in value b) required: _$1(in value b) val x = b.three(b.two) If I am interpreting this correctly, the compiler thinks that b in method test has a type that looks like this (not legal syntax, but hopefully clearer): trait Foo { def two: X where ∃ X >: T def three(x: X where ∃