self-type

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

点点圈 提交于 2019-11-30 04:45:50
问题 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

scala self-type: value is not a member error

[亡魂溺海] 提交于 2019-11-28 13:12:33
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 = { val (a, b, c) = (v.x, v.y, v.z) new Vec3(c * y - b * z, a * z - c * x, b * x - a * y) } def perpTo(v

can a scala self type enforce a case class type

痴心易碎 提交于 2019-11-28 12:39:17
Would there be any way in scala, to define a trait's self type to be a case class, as in "any case class"? I would like a self type to be able to use the .copy method of a case class, enforcing that its self type is some case class not a regular class. Structural types, I think, won't help, as they require a signature comprising specific arguments (I can probably not structural-type generically for any case class ). Please forgo the "if you need that you must be doing something wrong", as I've already moved on but my api design - will have been slicker if the above were possible. I am also

Difference between trait inheritance and self type annotation

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:43:53
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? 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 { this : FooService => } trait FooPersistence { this : FooService => } object Services extends FooService with

can a scala self type enforce a case class type

做~自己de王妃 提交于 2019-11-27 07:07:08
问题 Would there be any way in scala, to define a trait's self type to be a case class, as in "any case class"? I would like a self type to be able to use the .copy method of a case class, enforcing that its self type is some case class not a regular class. Structural types, I think, won't help, as they require a signature comprising specific arguments (I can probably not structural-type generically for any case class ). Please forgo the "if you need that you must be doing something wrong", as I

Explicit self-references with no type / difference with ''this''

梦想的初衷 提交于 2019-11-27 04:02:33
I understand the use for explicitly typed self-references : trait T { self : T2 => ... } In the body, self is an alias for this but has the more precise type T with T2 . Now, I've seen this in code: trait T { self => ... } That is, an explicit self reference with no additional type information. In this configuration, is there any situation in which self is not just an alias for this ? It is an alias for this . Your first example is useful for ensuring that the trait has been mixed in to an appropriate type, and makes those methods available. The second example is useful when you have inner

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

末鹿安然 提交于 2019-11-27 00:14:11
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 class Abstract0[T <: Abstract0[T]] because it makes it harder for clients to handle this type. I tried

Explicit self-references with no type / difference with &#39;&#39;this&#39;&#39;

∥☆過路亽.° 提交于 2019-11-26 11:01:28
问题 I understand the use for explicitly typed self-references: trait T { self : T2 => ... } In the body, self is an alias for this but has the more precise type T with T2 . Now, I\'ve seen this in code: trait T { self => ... } That is, an explicit self reference with no additional type information. In this configuration, is there any situation in which self is not just an alias for this ? 回答1: It is an alias for this . Your first example is useful for ensuring that the trait has been mixed in to

What is the difference between self-types and trait subclasses?

柔情痞子 提交于 2019-11-26 00:12:09
问题 A self-type for a trait A : trait B trait A { this: B => } says that \" A cannot be mixed into a concrete class that does not also extend B \" . On the other hand, the following: trait B trait A extends B says that \"any (concrete or abstract) class mixing in A will also be mixing in B\" . Don\'t these two statements mean the same thing? The self-type seems to serve only to create the possibility of a simple compile-time error. What am I missing? 回答1: It is predominately used for Dependency