Why Scala let's us declare an abstract class as final?

I know, it’s illogical to ask but still, I tried declaring a class as abstract and final and I was able to do that.
Does anyone know why is this possible? I don’t see any logical usage of such construct.

Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> abstract final class Person
defined class Person

scala> class Employee extends Person
<console>:12: error: illegal inheritance from final class Person
       class Employee extends Person


You could still use it as a type parameter, e.g.

final abstract class Person
trait Namer[T] { def name: String}
val personNamer = new Namer[Person] { def name: String = “person” }
def [T] printName(namer: Namer[T]) = println(s"It’s a ${namer.name}!")

A type never instantiated but still playing a role in type-checking it
called a phantom type.

 Best, Oliver
1 Like

Kudos to you! I couldn’t come up with this Typeclass case, interesting! :slight_smile: Thank you.

Not a use case:

scala> abstract final class C { final val c = 42 }
defined class C

scala> def f(x: C) = x.c
f: (x: C)Int

scala> f(null)
res0: Int = 42

scala> f(null)
res0: Int = 42

That’s a little bit disturbing.

Great scott.

I’d be really interested in an explanation of why this happens.

Premature optimization? :slight_smile:

My guess is the compiler does something like this if you continue the REPL session.

scala> null.asInstanceOf[C]
res1: C = null

scala> res1.c
res2: Int = 42

Constant value definitions always get inlined. So null never gets dereferenced.

1 Like

Is this issue just related then? https://github.com/scala/bug/issues/8097 Edited to point to new issue tracker.