f-bounded-polymorphism

Understanding type arguments do not conform to class type parameter bounds error when using Higher kinded type parameter

你。 提交于 2021-01-29 21:55:54
问题 I am trying to understand why the following piece of code won't compile when I use a higher kinded type parameter for T in MyModel abstract class Model[M <: Model[M]] class MyModel[T] extends Model[MyModel[T]] class Bar[TModel <: Model[TModel]] object Foo extends App { new Bar[MyModel[_]] } But if i change it to new Bar[MyModel[Any]] it will compile. Why is this ? 回答1: Bar[MyModel[_]] is Bar[MyModel[X] forSome {type X}] . (It should not be confused neither with Bar[MyModel[X]] forSome {type X

How to combine F-bounded polymorphism with associated types in Scala?

牧云@^-^@ 提交于 2020-06-01 05:49:11
问题 I have a trait called Graphlike for things that work as a graph. Notably, one of the properties I want to have is that the method g.subgraph(Set(1, 2, 3)) would return a subgraph of the same type with just the vertices 1, 2 and 3. Apparently, this means that I want F-bounded polymorphism and that Graphlike looks something like this: trait Graphlike[A <: Graphlike[A]] { type Vertex def subgraph(selectedVertices: Set[Vertex]): A } I also have a trait representing an automaton with associated

Advantages of F-bounded polymorphism over typeclass for return-current-type problem

て烟熏妆下的殇ゞ 提交于 2020-03-13 13:39:45
问题 Returning the current type questions are often asked on StackOverflow. Here is one such example. The usual answers seem to be either F-bounded polymorphism or typeclass pattern solution. Odersky suggests in Is F-bound polymorphism useful? F-bounds do indeed add significant complexity. I would love to be able to get rid of them, and replace them with higher-kinded subtyping whilst tpolecat (the author of linked post) suggests A better strategy is to use a typeclass, which solves the problem

Advantages of F-bounded polymorphism over typeclass for return-current-type problem

喜夏-厌秋 提交于 2020-03-13 13:39:30
问题 Returning the current type questions are often asked on StackOverflow. Here is one such example. The usual answers seem to be either F-bounded polymorphism or typeclass pattern solution. Odersky suggests in Is F-bound polymorphism useful? F-bounds do indeed add significant complexity. I would love to be able to get rid of them, and replace them with higher-kinded subtyping whilst tpolecat (the author of linked post) suggests A better strategy is to use a typeclass, which solves the problem

Scala Type mismatch when a generic type operates on the same generic type

主宰稳场 提交于 2020-01-15 10:55:09
问题 I have an generic case class Route that takes in a List of subclasses of Location. However in the following method I get a type mismatch in the call to distance expected: head.T, actual: T case class Route[T <: Location](route: List[T]) { def measureDistance: Double = { def measure(head: T, tail: List[T], acc: Double = 0.0): Double = tail match { case Nil => acc case h :: t => measure(h, t, head.distance(h) + acc) } if (route.isEmpty) 0.0 else measure(route.head, route.tail) } } The basic

Scala F-bounded polymorphism on object

眉间皱痕 提交于 2019-12-09 12:48:48
问题 I cannot write the following F-bounded polymorphism in Scala. Why? trait X[T <: X[T]] object Y extends X[Y] How can I express this and make it compile? 回答1: It really seems like you ought to be able to write, trait X[T <: X[T]] object Y extends X[Y.type] however, if you try that the compiler will give you an unhelpful (and I think spurious) error, scala> object Y extends X[Y.type] <console>:16: error: illegal cyclic reference involving object Y object Y extends X[Y.type] I say "spurious"

Scala F-bounded polymorphism on object

谁说胖子不能爱 提交于 2019-12-03 15:04:34
I cannot write the following F-bounded polymorphism in Scala. Why? trait X[T <: X[T]] object Y extends X[Y] How can I express this and make it compile? It really seems like you ought to be able to write, trait X[T <: X[T]] object Y extends X[Y.type] however, if you try that the compiler will give you an unhelpful (and I think spurious) error, scala> object Y extends X[Y.type] <console>:16: error: illegal cyclic reference involving object Y object Y extends X[Y.type] I say "spurious" because we can construct an equivalent object with a little bit of additional infrastructure, trait X[T <: X[T]]

Derived curiously recurring templates and covariance

独自空忆成欢 提交于 2019-11-30 03:25:01
问题 Suppose I have a base class which cloning of derived classes: class Base { public: virtual Base * clone() { return new Base(); } // ... }; I have a set of derived classes which are implemented using a curiously recurring template pattern: template <class T> class CRTP : public Base { public: virtual T * clone() { return new T(); } // ... }; And I attempt to derive from that further like this: class Derived : public CRTP<Derived> { public: // ... }; I get compilation errors to the effect of:

Attempting to model F-bounded polymorphism as a type member in Scala

巧了我就是萌 提交于 2019-11-28 23:29:43
I wanted to try writing a type whose methods can be homogeneous and return values of the same type: object SimpleTest { trait Foo extends Product with Serializable { type Self <: Foo def bar: Self } case class X() extends Foo { type Self = X def bar = this } case class Y() extends Foo { type Self = Y def bar = this } trait TC[A] implicit val tc: TC[Foo] = new TC[Foo] { } def tester[A: TC](x: Seq[A]) = "foo" // tester(Seq(X(), Y())) } Unfortunately, the commented-out line calling tester fails with the following error (Scala 2.10): Error: could not find implicit value for evidence parameter of

Attempting to model F-bounded polymorphism as a type member in Scala

前提是你 提交于 2019-11-27 21:20:49
问题 I wanted to try writing a type whose methods can be homogeneous and return values of the same type: object SimpleTest { trait Foo extends Product with Serializable { type Self <: Foo def bar: Self } case class X() extends Foo { type Self = X def bar = this } case class Y() extends Foo { type Self = Y def bar = this } trait TC[A] implicit val tc: TC[Foo] = new TC[Foo] { } def tester[A: TC](x: Seq[A]) = "foo" // tester(Seq(X(), Y())) } Unfortunately, the commented-out line calling tester fails