This page is no longer maintained — Please continue to the home page at www.scala-lang.org

AnyVal's non-abstract finality

2 replies
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

Anyone know why AnyVal is marked FINAL, and not marked ABSTRACT? I fixed
a bug which brought this to my attention because AnyVal is marked
SEALED, which post-bugfix means that this method:

def getAnyValClass(x: AnyVal): JClass[_] = x match {
case _: Byte => classOf[Byte]
case _: Short => classOf[Short]
case _: Int => classOf[Int]
case _: Long => classOf[Long]
case _: Float => classOf[Float]
case _: Double => classOf[Double]
case _: Char => classOf[Char]
case _: Boolean => classOf[Boolean]
case _: Unit => classOf[Unit]
}

appears non-exhaustive since it doesn't cover AnyVal.

However AnyVal has no constructor so I don't know where another type is
going to come from.

I tried setting the ABSTRACT flag on it and everything built normally.
I figured it would, was mostly afraid that the combo of FINAL and
ABSTRACT would distress it.

Can I make it abstract?

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: AnyVal's non-abstract finality

On Sat, Oct 17, 2009 at 6:32 AM, Paul Phillips wrote:
> Anyone know why AnyVal is marked FINAL, and not marked ABSTRACT? I fixed
> a bug which brought this to my attention because AnyVal is marked
> SEALED, which post-bugfix means that this method:
>
>  def getAnyValClass(x: AnyVal): JClass[_] = x match {
>    case _: Byte    => classOf[Byte]
>    case _: Short   => classOf[Short]
>    case _: Int     => classOf[Int]
>    case _: Long    => classOf[Long]
>    case _: Float   => classOf[Float]
>    case _: Double  => classOf[Double]
>    case _: Char    => classOf[Char]
>    case _: Boolean => classOf[Boolean]
>    case _: Unit    => classOf[Unit]
>  }
>
> appears non-exhaustive since it doesn't cover AnyVal.
>
> However AnyVal has no constructor so I don't know where another type is
> going to come from.
>
> I tried setting the ABSTRACT flag on it and everything built normally.
> I figured it would, was mostly afraid that the combo of FINAL and
> ABSTRACT would distress it.
>
> Can I make it abstract?
>
I see no reason why not. Logically it should be ABSTRACT and SEALED.
But without experimentation I would not knowwhether the change from
FINAL to SEALED is feasible.

Cheers

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: AnyVal's non-abstract finality

On Sat, Oct 17, 2009 at 10:17:41AM +0200, martin odersky wrote:
> I see no reason why not. Logically it should be ABSTRACT and SEALED.
> But without experimentation I would not knowwhether the change from
> FINAL to SEALED is feasible.

Experimentation says it's fine. About the only observable change I
could see were new (better, even) error messages to these doomed
attempts. We go from this:

scala> new AnyVal
:5: error: AnyVal does not have a constructor
new AnyVal
^

scala> new AnyVal {}
:5: error: illegal inheritance from final class AnyVal
new AnyVal {}
^

to this:

scala> new AnyVal
:5: error: class AnyVal is abstract; cannot be instantiated
new AnyVal
^

scala> new AnyVal {}
:5: error: illegal inheritance from sealed class AnyVal
new AnyVal {}
^

I checked that in with my fix for #424242.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland