Questions tagged [f-bounded-polymorphism]

Interfaces or classes having a type parameter which is a subtype of the interface itself, in any language; also known as the "curiously recurring template pattern". It helps defining chaining methods in a hierarchy of classes, or typed cloning methods.

A F-bounded polymorphism allows an abstract class, an interface or a trait to access the type of its implementation. It can be useful to define methods in the abstract class that return the type of the subclass itself, such as for cloning, chaining two methods, etc.

Here is an example for the clone method.

Java

abstract class Base<T extends Base<T>> {
    public T clone(T original);
}
class Derived extends Base<Derived > {
    @Override public A clone(A original) {
       //...
    }
}

Scala

trait Base[T <: Base[T]] {
  def clone(): T
}
class Derived extends Base[Derived] {
  def clone(): Derived = //...
}

C++

template<class T>
class Base
{
    // ...
};
class Derived : public Base<Derived>
{
    // ...
};

Wikipedia pages dealing with it:
Bounded quantification
Curiously recurring template pattern

49 questions
44
votes
5 answers

Practical Uses for the "Curiously Recurring Template Pattern"

What are some practical uses for the "Curiously Recurring Template Pattern"? The "counted class" example commonly shown just isn't a convincing example to me.
Kevin
  • 22,827
  • 15
  • 51
  • 56
15
votes
1 answer

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

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 { …
Mysterious Dan
  • 1,296
  • 10
  • 24
9
votes
3 answers

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

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…
Mario Galic
  • 41,266
  • 6
  • 39
  • 76
9
votes
2 answers

Derived curiously recurring templates and covariance

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…
Www
  • 282
  • 1
  • 12
8
votes
2 answers

scala f-bounded types explanation

After going through a few examples, I have to say, I fail to understand what the F-Bounded polymorphic brings. To use the example from scala school (https://twitter.github.io/scala_school/advanced-types.html#fbounded) They explain that they need…
jeremie
  • 718
  • 7
  • 12
6
votes
2 answers

Scala F-bounded polymorphism on object

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?
Mikaël Mayer
  • 9,351
  • 5
  • 58
  • 96
6
votes
1 answer

Expressing a F bounded type as abstract type member

I want to convert F bounded polymorphism to abstract type members. trait FBoundedMovable[Self <: FBoundedMovable[Self]] { def moveTo(pos: Vect2): Self } to trait Movable { self => type Self <: (Movable { type Self = self.Self }) def…
5
votes
2 answers

Resolving types in F-bounded polymorphism

I have these models: trait Vehicle[T <: Vehicle[T]] { def update(): T } class Car extends Vehicle[Car] { def update() = new Car() } class Bus extends Vehicle[Bus] { def update() = new Bus() } If I obtain an instance of a Vehicle[Car] and invoke…
slouc
  • 8,715
  • 3
  • 13
  • 36
4
votes
1 answer

How to go from a wildcard type to a parameterized type?

Let's say there's a framework type Row with multiple type parameters, and a method that works with instances of the Row type and uses all these type parameters. I have a method that works with any type of Row, even different types at the same time,…
fejesjoco
  • 11,307
  • 3
  • 31
  • 63
4
votes
1 answer

How to properly define existential for a f-bound polymorphic type

There are couple of similar questions here, I read them and found no answer that could get my code work. I suppose I hit a corner case that requires more precise type specification than normal. My case in two words. I'd like to build very simple…
3
votes
1 answer

Why is f-bounded polymorphism im Scala commonly implemented with an upper type bound as well as a self type

Why is f-bounded polymorphism in Scala commonly implemented with an upper type bound as well as a self type like trait MyTrait[A <: MyTrait[A]] { self: A => … } and not simply with just a self type like trait MyTrait[A] { self: A => … } The…
mrArkwright
  • 243
  • 2
  • 10
3
votes
2 answers

Is there any way to return polymorphic this in Java?

In Typescript, there is this concept of a polymorphic return type this. https://www.typescriptlang.org/docs/handbook/advanced-types.html#polymorphic-this-types Example: export abstract class Animal { private name: string; public…
3
votes
2 answers

Compilation issue in Scala with F-bounded types and existential types

I'm using a F-bounded type in order to be able to return the current type trait Board[T <: Board[T]] { def updated : T } And I'm trying to write a generic helper method that use it. The question is : why the following do not compile ? object…
3
votes
1 answer

Virtual method with variadic arguments

I am trying to implement a logging system abstracted behind a locator service (in the style of this guide) in such a way that the logging system can be subclassed for various different logging situations. I would prefer to be able to use printf…
Orpheon
  • 161
  • 2
  • 9
3
votes
1 answer

Why scala fails type inference for f-bound polymorphism?

Simple example to illustrate the issue: trait WTF[W <: WTF[W]] { def get : Int } trait Zero extends WTF[Zero] { override def get : Int = 0 } case object Zero extends Zero final case class Box(inner : Int) extends WTF[Box] { override def get…
ayvango
  • 5,627
  • 3
  • 28
  • 70
1
2 3 4