abstract override on types

51 views
Skip to first unread message

Eugene Burmako

unread,
Dec 10, 2012, 10:46:05 AM12/10/12
to scala-internals
scala> trait T { type U }
defined trait T

scala> trait T1 extends T { abstract override type U = Int }
defined trait T1

Does this make sense to you?

Paul Phillips

unread,
Dec 10, 2012, 10:55:58 AM12/10/12
to scala-i...@googlegroups.com
On Mon, Dec 10, 2012 at 7:46 AM, Eugene Burmako <eugene....@epfl.ch> wrote:
scala> trait T1 extends T { abstract override type U = Int }
defined trait T1

Does this make sense to you?

That's funny, you've discovered a way to implement the periodically wished for "abstract type which acts abstractly" - in general scala won't stop you from instantiating classes even when the abstract types have never been made concrete, but it will here:

scala> new T1 { }
<console>:10: error: object creation impossible, since type U in trait T1, which equals Int is marked `abstract' and `override', but no concrete implementation could be found in a base class
              new T1 { }
                  ^

scala> trait T2 extends T { type U = Int }
defined trait T2

scala> new T with T2 with T1 { }
res1: T with T2 with T1 = $anon$1@2a7a17a6

I realize that doesn't answer your question. It does not not make sense, if that helps. Does it make sense to you?

Eugene Burmako

unread,
Dec 10, 2012, 11:11:26 AM12/10/12
to <scala-internals@googlegroups.com>
To me the concept of types being abstract override sounds strange, because from what I understand abstract override is about being able to delegate to abstract methods from a super class/trait.

Also the spec says: "The override modifier has an additional significance when combined with the abstract modifier. That modifier combination is only allowed for value members of traits".

Paul Phillips

unread,
Dec 10, 2012, 11:27:14 AM12/10/12
to scala-i...@googlegroups.com


On Mon, Dec 10, 2012 at 8:11 AM, Eugene Burmako <eugene....@epfl.ch> wrote:
Also the spec says: "The override modifier has an additional significance when combined with the abstract modifier. That modifier combination is only allowed for value members of traits".

Oh, well that's all you needed. I had also consulted the spec and everything I found about abstract override spoke only in terms of "members"; the snippet you cite above unfortunately lacks the phrase "abstract override" so it eludes a search.

Tom Switzer

unread,
Dec 10, 2012, 11:53:46 AM12/10/12
to scala-i...@googlegroups.com
That doesn't seem terrible useful. It could be useful if it were used in the same context as I use abstract override.

trait T {
  type U
  def get: U
}

trait MaybeT extends T {
  abstract override type U = Option[super.U]
  abstract override def get: U = if (System.currentTimeMillis % 2 == 0) Some(super.get) else None
}

Not that this is a great example, but it is only really useful if you have access to the type of U in the super trait.

martin odersky

unread,
Dec 10, 2012, 3:34:26 PM12/10/12
to scala-internals
Yes, looks like a bug. - Martin

--
Martin Odersky
Prof., EPFL and Chairman, Typesafe
PSED, 1015 Lausanne, Switzerland
Tel. EPFL: +41 21 693 6863
Tel. Typesafe: +41 21 691 4967

Eugene Burmako

unread,
Dec 10, 2012, 3:39:36 PM12/10/12
to <scala-internals@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages