Scala: Abstract Types and Mixins - scala

Say I have two classes with the same identifier for a parameterized type
trait A {
type O
def f(a: Int, os: Seq[O]): Int
}
trait B {
type O
def g(a: Int, o: O): Int = { h1(o) + h2(a) }
def h1(o: O): Int
def h2(a: Int): Int = {a/2}
}
I would like to create a child class that will "marry" the two
trait C extends A with B {
def f(a: Int, os: Seq[O]): Int = {
os.map{ o => g(a, o) }.sum
}
}
Finally, I create an implementation for C
class D extends C {
type O = Int
def h1(o: O): Int = {5 * o}
}
At writing of C I don't yet know what type O is -- however, I'd like to constrain A.O == B.O such that it "makes sense" to use B.g in an implementation of A.f. I tried implementing this and it surprisingly seemed like Scala assumed there was only ever one type O
val d = new D
println(d.f(1, Seq(1,2,3)))
To me this seems incorrect -- why should A.O and B.O agree?
EDIT
I'd also like to note that If you were to instead put constraints on O like so,
case class Z()
case class Z1() extends Z
case class Z2() extends Z1
trait A {
type O <: Z
}
trait B {
type O >: Z2
}
class D extends C {
type O = Z1
Compilation will fail. However, if you put this instead,
trait A {
type O <: Z
}
trait B {
type O <: Z1
}
class D extends C {
type O = Z2
Compilation succeeds and everything runs fine.

I think Scala always "marries" the members -- both type and value members -- of traits when both are mixed in:
trait A { type T = Int }
trait B { type T = String }
object C extends A with B
gives
overriding type T in trait A, which equals Int;
type T in trait B, which equals String needs `override' modifier
(it's how Scala deals with the multiple inheritance issue -- no matter how many times an identifier is mixed in, it only exists once).

You second example fails because Scala needs a bit of help in establishing the type bounds in C. I suppose maybe it should be smart enough to figure it out on its own, but someone more versed in type theory would have to explain why or why not. This definition for C should work:
trait C extends A with B {
type O >: Z2 <: Z // restrict O's type so it's valid for both A and B
}
Scala's handling of abstract types is similar to its handling of abstract methods. Methods of the same name (and signature) that are pulled in from multiple traits are overridden together. Take the classic counter-example, derived from an example given in Ye Olde C++ Programming Language.
trait Cowboy {
/** Shoot something */
def draw
}
trait Window {
/** Draw something */
def draw
}
class CowboyWindow {
/** Probably best to draw something on the screen, then shoot it */
def draw {
// ...
}
}
In the same way that it assumes that there's only one type O in your code, it assumes there's only one method draw in this code. In this instance, this assumption is problematic, since you could end up shooting yourself in the foot.
As you discovered in your example, usually this assumption works out for the best (think about multiple interfaces declaring a close() method). But if this assumption is problematic for some class, as it is for CowboyWindow, you can replace inheritance with composition to work around it.

Related

Inherit from a class parametrized by an inner type

I would like to have a class B that inherits from a generic class A that is parametrized by an inner type of B. Specifically, I would like this (minimized example):
class A[T]
class B extends A[T] {
class T
}
Written like this, the compiler does not accept it. Is there any way to specify this inheritance relationship? (Using some different syntax, or some tricks.)
If not, what would be an official reference documenting that this is not possible?
Notes:
Yes, I want T to be an inner class. I do want separate instances of B to have incompatible types T.
This question considers the same situation but does not ask whether/how it is possible, but instead asks for other ways to write the same thing. (At least that's how the answers seem to interpret it.) In any case, that question is ten years old, and Scala may have evolved since.
Clarification: I want B to inherit from A, not from an inner class of A, or have an inner class of B inherit from A. The reason is that I want to use it in the following situation: A is a type class. B is supposed to provide a quick way to easily generate new classes of that type class (with minimal boilerplate). I want to the user of the class to do something like: implicit val X = createBInstance(), and then they will be able to use the type X.T and it will have type class A. The best I managed so far is to support the pattern val X = createBInstance(); implicit val xTypeclass = X.typeclass and use X.T. This means more boilerplate, the possibility to forget something, and it pollutes the namespace more (additional name xTypeclass). (For the very curious: A is MLValue.Converter, B is QuickConverter, and X is Header/RuntimeError/... in my code.)
You can try
class A {
type T
}
class B extends A {
class T
}
val b: B = new B
val t: b.T = new b.T
I made T a type member rather than type parameter.
Here a class overrides a type.
If you want to use T also as a type parameter you can introduce Aux-type
object A {
type Aux[_T] = A { type T = _T }
}
and use type A.Aux[T] as a replacement to your original A[T].
Now you're doing
trait A[X]
class B {
class T
/*implicit*/ object typeclass extends A[T]
}
val b = new B
implicit val b_typeclass: b.typeclass.type = b.typeclass
val b1 = new B
implicit val b1_typeclass: b1.typeclass.type = b1.typeclass
implicitly[A[b.T]]
implicitly[A[b1.T]]
Please notice that making object typeclass implicit is now useless.
Making object typeclass implicit would be useful if you made b, b1 objects rather than values
trait A[X]
class B {
class T
implicit object typeclass extends A[T]
}
object b extends B
object b1 extends B
implicitly[A[b.T]]
implicitly[A[b1.T]]
If you want to make B extend A then as earlier I propose to replace type parameter of A with a type member. A will still be a type class, just type-member type class rather than type-parameter type class
trait A {
type X
}
object A {
type Aux[_X] = A {type X = _X}
}
class B extends A {
type X = T
class T
}
implicit object b extends B
implicit object b1 extends B
implicitly[A.Aux[b.T]]
implicitly[A.Aux[b1.T]]

Is it possible to put an upper type bound mandating that a class has a concrete definition of a member type?

Let's consider:
trait Abstract {
type T
def get :T
def set(t :T) :Unit
}
class Concrete[X](var x :X) extends Abstract {
override type T = X
override def get :T = x
override def set(t :T) = x = t
}
class Generic[M <: Abstract](val m :M) {
def get :M#T = m.get
def set(t :M#T) :Unit = m.set(t)
def like[N <: Abstract](n :N) :Generic[N] = new Generic(n)
def trans[N <: Abstract](n :N) :N#T = like[N](n).get
}
Class Generic rightfully won't compile here because M#T can be literally any type in existence and not m.T. This can be 'fixed' in two ways:
def set(t :m.T) :Unit = ???
or
class Generic[M <: Abstract with Singleton]
First is unfeasible because in practice Generic might not even have an instance of M to use its member type (which happens in my real-life case), and passing around path dependent types in a compatible way between classes and methods is next to impossible. Here's one of the examples why path-dependent types are too strong (and a huge nuissance):
val c = new Concrete[Int](0)
val g = new Generic[c.type](c)
c.set(g.get)
The last line in the above example does not compile, even though g :Generic[c.type] and thus get :c.type#T which simplifies to c.T.
The second is somewhat better, but it still requires that every class with a M <: Abstract type parameter has an instance of M at instantiation and capturing m.T as a type parameter and passing everywhere a type bound Abstract { type T = Captured } is still a huge pain.
I would like to put a type bound on T specifying that only subclasses of Abstract which have a concrete definition of T are valid, which would make m.T =:= M#T for every m :M and neatly solve everything. So far, I haven't seen any way to do so.
I tried putting a type bound:
class Generic[M <: Abstract { type T = O } forSome { type O }]
which seemingly solved the issue of set, but fails with trans:
def like[N <: Abstract { type T = O } forSome { type O }](n :N) :Generic[N] = ???
def trans[N <: Abstract { type T = O } forSome { type O}](n :N) :N#T = ???
which seems to me like a clear type system deficiency. Additionally, it actually makes the situation worse in some cases, because it defines T early as some O(in class Generic) and won't unify it when additional constraints become available:
def narrow[N <: M { type T = Int }](n :N) :M#T = n.get
The above method will compile as part of class Generic[M <: Abstract], but not Generic[M <: Abstract { type T = O } forSome { type O}]. Replacing Abstract { type T = O } forSome { type O } with Concrete[_] yields very similar results. Both of these classes could be fixed by introducing a type parameter for T:
class Generic[M <: Abstract { type T = O }, O]
class Generic[M <: Concrete[_]]
Unfortunately, in several places I rely on two-argument type constructors such as
trait To[-X <: Abstract, +Y <: Abstract]
and use the infix notation X To Y as part of my dsl, so changing them to multi-argument, standard generic classes is not an option.
Is there a trick to get around this problem, namely to narrow down legal type parameters of a class so that their member type (or type parameter, I don't care) are both expressible as types and compatible with each other?. Think of M as some kind of factory of values of T and of Generic as its input data. I would like to parameterize Generic in such a way, that its instances are dedicated to concrete implementation classes of M. Do I have to write a macro for it?
I'm not sure exactly why you're saying that using a path dependent type will be annoying here, so maybe my answer will be completely off, sorry about that.
What's the problem with this?
class Generic[M <: Abstract](val m: M) {
def get: M#T = m.get
def set(t: m.T): Unit = m.set(t)
}
object Generic {
def like[N <: Abstract](n: N): Generic[N] = new Generic(n)
def trans[N <: Abstract](n :N): N#T = like(n).get
}
class Foo
val f = new Generic(new Concrete(new Foo))
f.set(new Foo)
You're saying you won't always have an instance m: M, but can you provide a real example of exactly what you want to achieve?

Possible bug in Scala 2.10.3 compiler

I ran into some code that looks pretty much like this:
object SomeOddThing extends App {
val c: C = new C
val a: A = c
val b: B = c
// these will all print None
println(a.x)
println(b.x)
println(c.x)
}
abstract class A {
def x: Option[String] = None
}
abstract class B extends A {
override def x: Option[String] = Some("xyz")
}
class C extends B {
// x now has type None.type instead of Option[String]
override def x = None
}
trait T {
this: B =>
override def x = Some("ttt")
}
// this won't compile with the following error
// error: overriding method x in class C of type => None.type;
// method x in trait T of type => Some[String] has incompatible type
// class D extends C with T {}
// ^
class D extends C with T {}
This looks like a bug to me. Either the type of C.x should be inferred correctly or the class shouldn't compile at all.
The spec 4.6.4 only promises to infer a conforming type for the result type of the overriding method.
See https://issues.scala-lang.org/browse/SI-7212 and be thankful you didn't throw.
Maybe it will change:
https://groups.google.com/forum/#!topic/scala-internals/6vemF4hOA9A
Update:
Covariant result types are natural. It's not grotesque that it infers what it would normally do. And that you can't widen the type again. It's maybe suboptimal, as that other issue puts it. (Speaking to my comment that it's an improvement rather than a bug per se.)
My comment on the ML:
This falls under "always annotate interface methods, including
interface for extension by subclass."
There are lots of cases where using an inferred type for an API method commits you to a type you had rather avoided. This is also true for the interface you present to subclasses.
For this problem, we're asking the compiler to retain the last explicitly ascribed type, which is reasonable, unless it isn't. Maybe we mean don't infer singleton types, or Nothing, or something like that. /thinking-aloud
scala> trait A ; trait B extends A ; trait C extends B
defined trait A
defined trait B
defined trait C
scala> trait X { def f: A }
defined trait X
scala> trait Y extends X { def f: B }
defined trait Y
scala> trait Z extends Y { def f: C }
defined trait Z
scala> trait X { def f: A = new A {} }
defined trait X
scala> trait Y extends X { override def f: B = new B {} }
defined trait Y
scala> trait Z extends Y { override def f: C = new C {} }
defined trait Z
scala> trait ZZ extends Z { override def f: A = new C {} }
<console>:13: error: overriding method f in trait Z of type => C;
method f has incompatible type
trait ZZ extends Z { override def f: A = new C {} }
^
To the question, what does it mean for None.type to conform to Option?
scala> implicitly[None.type <:< Option[_]]
res0: <:<[None.type,Option[_]] = <function1>
to show that None.type, the singleton type of the None object, is in fact an Option.
Usually, one says that the compiler avoids inferring singleton types, even when you kind of want it to.
Programming in Scala says, "Usually such types are too specific to be useful."

Taming the Scala type system

I don't seem to understand the Scala type system. I'm trying to implement
two base traits and a trait for a family of algorithms to work with them.
What am I doing wrong in the below?
The base traits for moves & states; these are simplified to just include
methods that expose the problem.
trait Move
trait State[M <: Move] {
def moves: List[M]
def successor(m: M): State[M]
}
Here's the trait for the family of algorithms that makes use of above.
I'm Not sure this is right!
There might be some +M / -S stuff involved...
trait Algorithm {
def bestMove[M <: Move, S <: State[M]](s: S): M
}
Concrete move and state:
case class MyMove(x: Int) extends Move
class MyState(val s: Map[MyMove,Int]) extends State[MyMove] {
def moves = MyMove(1) :: MyMove(2) :: Nil
def successor(p: MyMove) = new MyState(s.updated(p, 1))
}
I'm on very shaky ground regarding the below, but the compiler seems to accept it...
Attempting to make a concrete implementation of the Algorithm trait.
object MyAlgorithm extends Algorithm {
def bestMove(s: State[Move]) = s.moves.head
}
So far there are no compile errors; they show up when I try to put all the parts together, however:
object Main extends App {
val s = new MyState(Map())
val m = MyAlgorithm.bestMove(s)
println(m)
}
The above throws this error:
error: overloaded method value bestMove with alternatives:
(s: State[Move])Move <and>
[M <: Move, S <: State[M]](s: S)M
cannot be applied to (MyState)
val m = MyAlgorithm.bestMove(s)
^
Update: I changed the Algorithm trait to use abstract type members, as
suggested. This solved the question as I had phrased it but I had
simplified it a bit too much. The MyAlgorithm.bestMove() method must be
allowed to call itself with the output from s.successor(m), like this:
trait Algorithm {
type M <: Move
type S <: State[M]
def bestMove(s: S): M
}
trait MyAlgorithm extends Algorithm {
def score(s: S): Int = s.moves.size
def bestMove(s: S): M = {
val groups = s.moves.groupBy(m => score(s.successor(m)))
val max = groups.keys.max
groups(max).head
}
}
The above gives now 2 errors:
Foo.scala:38: error: type mismatch;
found : State[MyAlgorithm.this.M]
required: MyAlgorithm.this.S
val groups = s.moves.groupBy(m => score(s.successor(m)))
^
Foo.scala:39: error: diverging implicit expansion for type Ordering[B]
starting with method Tuple9 in object Ordering
val max = groups.keys.max
^
Do I have to move to an approach using traits of traits, aka the Cake pattern, to make this work? (I'm just guessing here; I'm thoroughly confused still.)
You declare MyAlgorithm#bestMove explicitly as taking a State[Move] parameter, but inside Main you are trying to pass it a MyState, which is a State[MyMove] not a State[Move].
You have a couple of options to resolve this. One would be to not constrain the types in MyAlgorithm:
object MyAlgorithm extends Algorithm {
def bestMove[M <: Move, S <: State[M]](s: S) : M = s.moves.head
}
Unfortunately, the scala type inference isn't smart enough to figure these types out for you, so at the call site, you have to declare them, making the call to MyAlgorithm#bestMove look like this:
val m = MyAlgorithm.bestMove[MyMove, MyState](s)
Another option use abstract type members of the Algorithm trait:
trait Algorithm {
type M <: Move
type S <: State[M]
def bestMove(s: S): M
}
And resolve the abstract types in the concrete implementation:
object MyAlgorithm extends Algorithm {
type M = MyMove
type S = MyState
def bestMove(s: S) : M = s.moves.head
}
Then the call site gets to return to your original version, without mentioning the types:
val m = MyAlgorithm.bestMove(s)
You may want to keep MyAlgorithm unaware of the actual types, and leave the determining of those types to the 'clients' of that object, in which case, change the object to a trait:
trait MyAlgorithm extends Algorithm {
def bestMove(s: S) : M = s.moves.head
}
Then in your Main class, you instantiate a MyAlgorithm with the concrete types:
val a = new MyAlgorithm {
type M = MyMove
type S = MyState
}
val m = a.bestMove(s)
Your comment "There might be some +M / -S stuff involved" was a good guess, but It won't work for you here. You might hope that the covariant type modifier "+" might help here. If you had declared the type parameter on State as
State[+M]
This would indicate that State[M] <:< State[N] if M <:< N. (read <:< as "is a subtype of"). Then you would have no problem passing in a State[MyMove] where a State[Move] was expected. However, you cannot use the covariant modifier on M here because it appears in the contravariant position as an argument to the successor function.
Why is this a problem? Your declaration of successor says that it will take an M and return a State. The covariant annotation says that a State[M] is also a State[Any]. So we should allow this assignment:
val x : State[Any] = y : State[MyMove]
Now if we have a State[Any], then x.successor is what type? Any => MyMove. Which cannot be correct, since your implementation is expecting a MyMove, not an Any
For updated code.
The compiler is very fair with complaints. Algorithm use one subclass of State as denoted and state successor may return any other subclass of State[M]
You may declare IntegerOne class
trait Abstract[T]
class IntegerOne extends Abstract[Int]
but the compiler have no clue that all instances of AbstractOne[Int] would be IntegerOne. It assume that there may be another class that also implements Abstract[Int]
class IntegerTwo extends Abstract[Int]
You may try to use implicit conversion to cast from Abstract[Int] to IntegerOne, but traits have no implicit view bounds as they have no value parameters at all.
Solution 0
So you may rewrite your Algorithm trait as an abstract class and use implicit conversion:
abstract class MyAlgorithm[MT <: Move, ST <: State[MT]] (implicit val toSM : State[MT] => ST) extends Algorithm {
override type M = MT // finalize types, no further subtyping allowed
override type S = ST // finalize types, no further subtyping allowed
def score(s : S) : Int = s.moves.size
override def bestMove(s : S) : M = {
val groups = s.moves.groupBy( m => score(toSM ( s.successor(m)) ) )
val max = groups.keys.max
groups(max).head
}
}
implicit def toMyState(state : State[MyMove]) : MyState = state.asInstanceOf[MyState]
object ConcreteAlgorithm extends MyAlgorithm[MyMove,MyState]
object Main extends App {
val s = new MyState(Map())
val m = ConcreteAlgorithm.bestMove(s)
println(m)
}
There are two drawbacks in this solution
using implicit conversion with asInstanceOf
tying types
You may extinguish first as the cost of further type tying.
Solution 1
Let use Algorithm as sole type parameterization source and rewrite type structure accordingly
trait State[A <: Algorithm] { _:A#S =>
def moves : List[A#M]
def successor(m : A#M): A#S
}
trait Algorithm{
type M <: Move
type S <: State[this.type]
def bestMove(s : S) : M
}
In that case your MyAlgorithm may be used without rewriting
trait MyAlgorithm extends Algorithm {
def score(s : S) : Int = s.moves.size
override def bestMove(s : S) : M = {
val groups = s.moves.groupBy(m => score(s.successor(m)))
val max = groups.keys.max
groups(max).head
}
}
Using it:
class MyState(val s : Map[MyMove,Int]) extends State[ConcreteAlgorithm.type] {
def moves = MyMove(1) :: MyMove(2) :: Nil
def successor(p : MyMove) = new MyState(s.updated(p,1))
}
object ConcreteAlgorithm extends MyAlgorithm {
override type M = MyMove
override type S = MyState
}
object Main extends App {
val s = new MyState(Map())
val m = ConcreteAlgorithm.bestMove(s)
println(m)
}
See more abstract and complicated usage example for this tecnique: Scala: Abstract types vs generics
Solution 2
There is also a simple solution to your question but I doubt it can solve your problem. You will eventually stuck upon type inconsistency once again in more complex use cases.
Just make MyState.successor return this.type instead of State[M]
trait State[M <: Move] {
def moves : List[M]
def successor(m : M): this.type
}
final class MyState(val s : Map[MyMove,Int]) extends State[MyMove] {
def moves = MyMove(1) :: MyMove(2) :: Nil
def successor(p : MyMove) = (new MyState(s.updated(p,1))).asInstanceOf[this.type]
}
other things are unchanged
trait Algorithm{
type M <: Move
type S <: State[M]
def bestMove(s : S) : M
}
trait MyAlgorithm extends Algorithm {
def score(s : S) : Int = s.moves.size
override def bestMove(s : S) : M = {
val groups = s.moves.groupBy(m => score(s.successor(m)))
val max = groups.keys.max
groups(max).head
}
}
object ConcreteAlgorithm extends MyAlgorithm {
override type M = MyMove
override type S = MyState
}
object Main extends App {
val s = new MyState(Map())
val m = ConcreteAlgorithm.bestMove(s)
println(m)
}
Pay attention to final modifier to MyState class. It ensures that conversion asInstanceOf[this.type] is correct one. Scala compiler may compute itself that final class keeps always this.type but it still have some flaws.
Solution 3
There is no need to tie Algorithm with custom State. As long as Algorithm does not use specific State function it may be written simpler without type bounding exercises.
trait Algorithm{
type M <: Move
def bestMove(s : State[M]) : M
}
trait MyAlgorithm extends Algorithm {
def score(s : State[M]) : Int = s.moves.size
override def bestMove(s : State[M]) : M = {
val groups = s.moves.groupBy(m => score(s.successor(m)))
val max = groups.keys.max
groups(max).head
}
}
This simple example doesn't come to my mind quickly because I've assumed that binding to different states are obligatory. But sometimes only part of system really should be parameterized explicitly and your may avoid additional complexity with it
Conclusion
Problem discussed reflects bunch of problems that arises in my practice very often.
There are two competing purposes that should not exclude each other but do so in scala.
extensibility
generality
First means that you can build complex system, implement some basic realization and be able to replace its parts one by one to implement more complex realization.
Second allows your to define very abstract system, that may be used for different cases.
Scala developers had very challenging task for creating type system for a language that can be both functional and object oriented while being limited to jvm implementation core with huge defects like type erasure. Co/Contra-variance type annotation given to users are insufficient for expressing types relations in complex system
I have my hard times every time I encounter extensiblity-generality dilemma deciding which trade-off to accept.
I'd like not to use design pattern but to declare it in the target language. I hopes that scala will give me this ability someday.

Can I declare a decorator class that extends its own type parameter?

I'd like to do something like this:
case class D[X <: A](arg1 : X, arg2: Int) extends X {
}
D is kind of a decorator class for arg1, and I'd like to apply it to several different kinds of things that are subclasses of A.
However I get this error:
scala> case class D[X <: A](arg1 : X, arg2: Int) extends X { override val name = "D"; }
:6: error: class type required but X found
If not, is there a more scalaish way to do this kind of thing?
The class that you extend has to be known at compile time and a type parameter is generally not. Therefore, it's not possible to do this.
However, if you're trying to extend X to benefit from the implementations of methods defined in an interface trait A, then you can mix-in X when instantiating the class.
new D with X
If you'd like to preserve the 'case class' features of D, then using D as a proxy which forwards calls to methods defined in A to the parameter arg1 of type X is one solution.
trait A {
def foo
}
case class D[X <: A](arg1: X) extends A {
def forw = arg1
def foo = forw.foo
}