- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
AnyVal's non-abstract finality
Sat, 2009-10-17, 05:32
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?
Sat, 2009-10-17, 13:47
#2
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.
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