self-type Questions
2
Solved
I have a trait with a self-type annotation that has a type parameter. This trait is from a library and cannot be modified. I want to pass this trait to a function that will require an upper bound f...
Albania asked 9/9, 2022 at 4:46
0
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...
3
I wonder what is the reasoning behind the following behaviour?
@ trait Bar
defined trait Bar
@ trait Foo { self: Bar => }
defined trait Foo
@ def x: Foo = ???
defined function x
@ val y: Bar ...
Kabul asked 18/7, 2020 at 12:7
6
Solved
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 ...
Mojave asked 8/2, 2010 at 21:17
2
Solved
I have some case classes which have a method tupled defined in its companion object. As it can be seen from the code below in companion objects, it is just code duplication.
case class Book(id: In...
Retort asked 19/1, 2016 at 15:29
1
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 tha...
1
Solved
From the definition of TreeNode in Spark SQL:
abstract class TreeNode[BaseType <: TreeNode[BaseType]] extends Product {
self: BaseType =>
...
}
What does it say about the subtypes of Tre...
Bullfight asked 11/10, 2015 at 7:55
11
Solved
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 ...
2
Solved
Is it possible to access values in the outer trait from an inner trait mixin? i.e.:
trait Outer {
val foo
trait Inner
}
trait InnerMixin { this: Outer#Inner =>
def bar {
// how can I acces...
Hiers asked 9/10, 2014 at 22:6
1
Solved
This was probably asked before, but I have this problem:
trait Container[+A] {
def a: A
def methodWithSideEffect() = {
// perform some side effecting work
this
}
}
class IntContainer(val a:...
Contrapositive asked 9/3, 2014 at 21:36
4
Solved
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 ...
Homily asked 6/2, 2013 at 13:14
3
A self type looks like the following example:
trait A { self: String => }
This says, that trait A (or a subtype of it) must inherit the class String.
The keyword self is followed by : analog...
1
Solved
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 s...
1
Scala has two instruments for expressing object composition: original self-type concept and well known trivial composition. I'm curios what situations I should use which in.
There are obvious diff...
Dichromaticism asked 16/6, 2012 at 1:39
3
Solved
I have two case classes that inherit from an abstract base class. I want to define some methods on the abstract base class that use the copy methods on the inheriting case classes (and so return an...
Lynn asked 24/4, 2012 at 19:30
2
Solved
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:...
Marylnmarylou asked 8/2, 2012 at 17:39
2
Solved
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 ...
1
Solved
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 ...
1
Solved
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 respo...
Cassycast asked 30/8, 2011 at 22:7
2
Solved
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 thi...
2
Solved
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):Doubl...
2
Solved
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...
Depopulate asked 30/11, 2010 at 11:18
1
Say I have the following traits:
trait A
trait B { this: A => }
trait C extends B // { this: A => }
Compiler error: illegal inheritance; self-type C does not conform to B's selftype B wi...
Hellcat asked 6/8, 2010 at 7:16
1
Solved
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, be...
Lambart asked 29/3, 2009 at 20:19
1
© 2022 - 2024 — McMap. All rights reserved.