self-type

Kotlin - how to return “self type” in a subclass? (without an extension function)

☆樱花仙子☆ 提交于 2021-01-01 04:19:15
问题 Let's have these classes: class A { fun foo(): A = this } class B: A() { fun bar() { ... } } Now I would like Kotlin to detect when I call foo on B , and give me the result typed as B . So that I can write: B().foo().bar() With kotlin 1.4.20, this is not possible - it would need an explicit cast to B after foo() . Perhaps it could be handled by the compiler, if it clearly sees that the function returns this . Or maybe we could hint it explicitly... class A { fun <Self> foo(): Self = this } I

Kotlin - how to return “self type” in a subclass? (without an extension function)

拜拜、爱过 提交于 2021-01-01 04:18:22
问题 Let's have these classes: class A { fun foo(): A = this } class B: A() { fun bar() { ... } } Now I would like Kotlin to detect when I call foo on B , and give me the result typed as B . So that I can write: B().foo().bar() With kotlin 1.4.20, this is not possible - it would need an explicit cast to B after foo() . Perhaps it could be handled by the compiler, if it clearly sees that the function returns this . Or maybe we could hint it explicitly... class A { fun <Self> foo(): Self = this } I

What is more Scala idiomatic: trait TraitA extends TraitB or trait TraitA { self: TraitB => }

自古美人都是妖i 提交于 2020-01-21 03:58:57
问题 Apart from the inheritance aspect, is there a difference between the following class templates: 1| trait TraitA extends TraitB 2| trait TraitA { self: TraitB => } I would like to split responsibilities between TraitA and TraitB but the former cannot function without the latter. How would you express this intent? To me solution [2] would be the more natural approach. However I do not want to put the burden on implementers mixing in what needs to be mixed in anyway. 回答1: My preference is

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

心不动则不痛 提交于 2020-01-11 05:58:15
问题 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

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

纵然是瞬间 提交于 2020-01-11 05:58:07
问题 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

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

流过昼夜 提交于 2019-12-18 19:00:49
问题 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

scala self-type: value is not a member error

纵饮孤独 提交于 2019-12-17 20:42:21
问题 This is a followup to this question. I'm trying to implement vectors in scala with a generic super class using self-types: trait Vec[V] { self:V => def /(d:Double):Vec[V] def dot(v:V):Double def norm:Double = math.sqrt(this dot this) def normalize = self / norm } Here's an implementation of a 3D vector: class Vec3(val x:Double, val y:Double, val z:Double) extends Vec[Vec3] { def /(d:Double) = new Vec3(x / d, y / d, z / d) def dot(v:Vec3) = x * v.x + y * v.y + z * v.z def cross(v:Vec3):Vec3 =

Difference between trait inheritance and self type annotation

梦想与她 提交于 2019-12-17 18:13:23
问题 In Scala, I've seen the constructs trait T extends S and trait T { this: S => used to achieve similar things (namely that the abstract methods in S must be defined before an instance may be created). What's the difference between them? Why would you use one over the other? 回答1: I'd use self-types for dependency-management: This trait requires another trait to be mixed in. And I'd use inheritance to refine another trait or interface. Just as an example: trait FooService trait FooRemoting {

How to use Scala's this typing, abstract types, etc. to implement a Self type?

可紊 提交于 2019-12-17 06:28:28
问题 I couldn't find the answer to this in any other question. Suppose that I have an abstract superclass Abstract0 with two subclasses, Concrete1 and Concrete1. I want to be able to define in Abstract0 something like def setOption(...): Self = {...} where Self would be the concrete subtype. This would allow chaining calls to setOption like this: val obj = new Concrete1.setOption(...).setOption(...) and still get Concrete1 as the inferred type of obj. What I don't want is to define this: abstract

Scala: self type annotation

↘锁芯ラ 提交于 2019-12-14 04:06:41
问题 (Trying to understand the use of self types by probing the borders.) This cannot be instantiated (D and String are classes, but one of them has to be mixed in. plus String is final.). But is there any other use for it? class D { foo: String => def f2 = foo.substring(1) } Update: Sorry, I seem to be not good at asking questions. What I want to know is whether this strange special case makes sense . The case where class D can never be instantiated, as 1. I cannot mix in String, as it is not a