self-type

scala self-type: member of type parameter error

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 16:07:33
问题 This is a followup to this question. Why does this code not compile, and how do I fix it? trait Vec[V] { self:V => def -(v:V):V def dot(v:V):Double def norm:Double = math.sqrt(this dot this) def dist(v:V):Double = (this - v).norm } The error is: Vec.scala:6: error: value norm is not a member of type parameter V def dist(v:V):V = (this - v).norm ^ 回答1: By changing the definition of - to def -(v:V):Vec[V] 回答2: The proper solution is: trait Vec[V <: Vec[V]] { self:V => def -(v:V):V def dot(v:V)

Java self-typed methods: cannot safely cast to actual type

被刻印的时光 ゝ 提交于 2019-12-12 04:04:47
问题 Consider the following class, which I believe is correctly called a self-typed class: public abstract class Example<E extends Example<E>> { /** Constructs an instance of the subclass */ protected abstract E construct(); /** Do a private operation in the base class */ private void specialOp() {} public E get1() { E obj = construct(); // Error: The method specialOp() from the type Example<E> is not visible obj.specialOp(); return obj; } public E get2() { Example<E> obj = construct(); obj

Scala self-type and generic class

我只是一个虾纸丫 提交于 2019-12-11 16:25:05
问题 abstract class Bar[M] { def print(t: M): Unit = { println(s"Bar: ${t.getClass()}") } } trait Foo[M] { this: Bar[M] => def print2(t: M): Unit = { println(s"Foo: ${t.getClass()}") } } object ConcreteBar extends Bar[Int] with Foo[Int] {} object ConcreteFooBar extends Bar[Int] with Foo[Int] {} object Test { def main(args: Array[String]): Unit = { ConcreteBar.print(1) ConcreteFooBar.print2(1) } In the example above, is there a way so that we don't have to repeat the type in the self-typed "bar"

Scala self type and this.type in collections issue

孤者浪人 提交于 2019-12-09 16:38:25
问题 I'm trying to wrap my head around abstract and explicit self types in scala. Lets consider this example: I want to create a base for extensible tree as simple as this: trait Tree { def children: Iterable[Tree] def descendants: Iterable[Tree] = { val dv = children.view; dv ++ (dv.flatMap { _.children }) } } However, I want to be able to extend tree nodes with some methods and use these methods like: tree.children foreach { _.newMethod() } For this I've tried: A. this.type: FAIL trait Tree {

How do I make sure a function receives the same parameter type as the current object?

浪子不回头ぞ 提交于 2019-12-08 07:02:43
问题 abstract class A { protected[this] type ThisType = this.type protected[this] type OtherType = this.type def assign(other: OtherType): ThisType } class B extends A { def assign(other: OtherType): ThisType = ??? } class C extends A { def assign(other: OtherType): ThisType = ??? } class D extends C { def assign(other: OtherType): ThisType = ??? } How do I make sure the other type received in assign of and object of type B is also B . e.g. how can I write something effectively like: def f1(p1: A,

How do I make sure a function receives the same parameter type as the current object?

♀尐吖头ヾ 提交于 2019-12-06 16:42:15
abstract class A { protected[this] type ThisType = this.type protected[this] type OtherType = this.type def assign(other: OtherType): ThisType } class B extends A { def assign(other: OtherType): ThisType = ??? } class C extends A { def assign(other: OtherType): ThisType = ??? } class D extends C { def assign(other: OtherType): ThisType = ??? } How do I make sure the other type received in assign of and object of type B is also B . e.g. how can I write something effectively like: def f1(p1: A, p2: A) = p1.assign(p2) def f2[T <: A](p1: T, p2: T) = p1.assign(p2) I am getting following errors: NB:

Scala: implementing method with return type of concrete instance

杀马特。学长 韩版系。学妹 提交于 2019-12-03 01:21:35
问题 I need a way to enforce a method in an abstract class to have a return type of the concrete class of the object it is called on. The most common example is a copy() method, and I'm currently using an approach based on abstract types: abstract class A(id: Int) { type Self <: A def copy(newId: Int): Self } class B(id: Int, x: String) extends A(id) { type Self = B def copy(newId: Int) = new B(newId, x) } class C(id: Int, y: String, z: String) extends A(id) { type Self = C def copy(newId: Int) =

Java field type for a value of a generically recursive self-type?

穿精又带淫゛_ 提交于 2019-12-01 06:32:43
Given a class hierarchy where the base class defines a recursive self-type: abstract class A<T extends A<T>> { } How can I declare another class (which should not be generic in T, because such a T could vary over the lifetime of the object) with a field that can hold any subclass of A? The following does not work: public class B { //fails to compile, because the capture of ? is not sufficiently narrow private A<?> a; public <T extends A<T>> setA(T a) { this.a = a; } } -- END OF QUESTION -- I've noticed a tendency of a number of StackOverflow members to approach certain difficult questions with

Does a class with a self type of another class make sense?

☆樱花仙子☆ 提交于 2019-11-30 20:48:13
scala> class A defined class A scala> class B {this: A => } defined class B scala> new B <console>:10: error: class B cannot be instantiated because it does not conform to its self-type B with A new B ^ Class B sets the self type to class A , therefore class B (or a subclass of it) has to extend class A to create an instance of B . But is this possible at all, since a subclass of B can only extend one class (and this is class B ) ? So this leads me to the question, does it make sense to declare the self type of a class to another class in any case ? 0__ You are right with the observation that

Scala: illegal inheritance; self-type Y does not conform to X's selftype SELF

耗尽温柔 提交于 2019-11-30 17:31:25
I have a trait, which takes a type parameter, and I want to say that the objects that implements this trait will also conform to this type parameter (using generics, for Java's compatibility) The following code: trait HandleOwner[SELF <: HandleOwner[SELF]] { self : SELF => // ... def handle: Handle[SELF] } trait Common[SELF <: Common[SELF]] extends HandleOwner[SELF] { // ... } Gives me the following error: illegal inheritance; self-type test.Common[SELF] does not conform to test.HandleOwner[SELF]'s selftype SELF If I change Common to: trait Common[SELF <: Common[SELF]] extends HandleOwner[SELF