type-members

Initialize a type variable with dynamic / concrete type

∥☆過路亽.° 提交于 2021-02-07 20:55:14
问题 I am learning Scala and I was trying to create a type class to solve the "Every animal eats food, but the type of food depends on the animal" problem. I have an Eats type class with context bounds: trait Eats[A <: Animal, B <: Edible] object Eats { def apply[A, B]: Eats[A, B] = new Eats[A, B] {} } with both Animal and Edible being abstract classes. The (reduced) Animal interface looks something like this abstract class Animal { type This // concrete type def eat[A <: Edible](food: A)(implicit

Initialize a type variable with dynamic / concrete type

故事扮演 提交于 2021-02-07 20:54:04
问题 I am learning Scala and I was trying to create a type class to solve the "Every animal eats food, but the type of food depends on the animal" problem. I have an Eats type class with context bounds: trait Eats[A <: Animal, B <: Edible] object Eats { def apply[A, B]: Eats[A, B] = new Eats[A, B] {} } with both Animal and Edible being abstract classes. The (reduced) Animal interface looks something like this abstract class Animal { type This // concrete type def eat[A <: Edible](food: A)(implicit

What's different between “def apply[T](c:T)” and “type T;def apply(c:T)”

℡╲_俬逩灬. 提交于 2020-11-29 21:17:37
问题 I have this program: object B{ def apply[T](c:T)={} } object C{ type T def apply(c:T)={} } object A extends App{ val d=B{println(1);2} val e=C{println(1);2} } the line val e = C{println(1);2} told me error:Type mismatch,expected C.T,actual:2 so why can't I write type T def apply(c:T) it seems the same as apply[T](c:T) and what's the type of T when I write val d=B{println(1);2} I can write many lines here! because T means generic,so it can be Int,String,user defined class Apple,Orange... and

What's different between “def apply[T](c:T)” and “type T;def apply(c:T)”

心不动则不痛 提交于 2020-11-29 21:17:00
问题 I have this program: object B{ def apply[T](c:T)={} } object C{ type T def apply(c:T)={} } object A extends App{ val d=B{println(1);2} val e=C{println(1);2} } the line val e = C{println(1);2} told me error:Type mismatch,expected C.T,actual:2 so why can't I write type T def apply(c:T) it seems the same as apply[T](c:T) and what's the type of T when I write val d=B{println(1);2} I can write many lines here! because T means generic,so it can be Int,String,user defined class Apple,Orange... and

What's different between “def apply[T](c:T)” and “type T;def apply(c:T)”

本秂侑毒 提交于 2020-11-29 21:11:30
问题 I have this program: object B{ def apply[T](c:T)={} } object C{ type T def apply(c:T)={} } object A extends App{ val d=B{println(1);2} val e=C{println(1);2} } the line val e = C{println(1);2} told me error:Type mismatch,expected C.T,actual:2 so why can't I write type T def apply(c:T) it seems the same as apply[T](c:T) and what's the type of T when I write val d=B{println(1);2} I can write many lines here! because T means generic,so it can be Int,String,user defined class Apple,Orange... and

Scala Abstract type members - inheritance and type bounds

落爺英雄遲暮 提交于 2020-01-01 12:25:10
问题 I ran into some strange situation in Scala today while I tried to refine the type bounds on an abstract type member. I have two traits that define bounds on a type member and combine them in a concrete class. That works fine but when matching / casting with the trait combination only one of the two TypeBounds is "active" and I struggle to understand why ... I tried to prepare an example: trait L trait R trait Left { type T <: L def get: T } trait Right { type T <: R } now if I combine these

Scala type inference for existential types and type members

元气小坏坏 提交于 2019-12-08 16:52:00
问题 The following piece of code does not compile : trait A[F] { def find(x: Int): F def fill(f: F): Unit } object TestA { def test[T <: A[F] forSome { type F }](t: T) = t.fill(t.find(0)) } It returns the following compilation error : test.scala:8: error: type mismatch; found : (some other)F(in type T) required: F(in type T) t.fill(t.find(0)) ^ However the following code complies just fine : trait B[F] { type R = F def find(x: Int): R def fill(f: R): Unit } object TestB { def test[T <: B[F]

Pattern Match on Case Objects with Type Members

五迷三道 提交于 2019-12-04 11:39:24
问题 Scala has a nice feature to infer type parameter inside the pattern match. It also checks pattern match exhaustiveness. For example: sealed trait PField[T] case object PField1 extends PField[String] case object PField2 extends PField[Int] def getValue[X](f: PField[X]): X = f match { case PField1 => "aaa" case PField2 => 123 } Is it possible to achieve the same but using type members instead of type parameters? sealed trait Field { type T } case object Field1 extends Field { type T = String }

Scala converting recursively bounded type parameter (F-bounded) to type member

风格不统一 提交于 2019-12-03 08:24:16
问题 How would I convert: trait Foo[A <: Foo[A]] to a type member? I.e., I want something along the lines of the following: trait Foo { type A <: Foo {type A = ???} } but I am having difficulty because the name A is already taken within the type refinement. This question is similar (and spawned from): F-bounded quantification through type member instead of type parameter? 回答1: Use a self-type: scala> trait Foo { self => type A <: Foo {type A = self.A}} defined trait Foo scala> class Bar extends