Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concrete classes can have abstract type members #1753

Closed
scabug opened this issue Feb 28, 2009 · 8 comments
Closed

Concrete classes can have abstract type members #1753

scabug opened this issue Feb 28, 2009 · 8 comments
Assignees
Labels

Comments

@scabug
Copy link

scabug commented Feb 28, 2009

The following compiles:

class Baz { type T; }

I'm not 100% sure I'm not missing something in the SLS about default of type members, but I can't find anything.

@scabug
Copy link
Author

scabug commented Feb 28, 2009

Imported From: https://issues.scala-lang.org/browse/SI-1753?orig=1
Reporter: @DRMacIver

@scabug
Copy link
Author

scabug commented Feb 28, 2009

Geoffrey Alan Washburn (washburn) said:
This is an intentional "feature" Martin added before 2.7.2 or 2.7.3 (I can't remember which) but was not announced or as you note described in the SLS.

@scabug
Copy link
Author

scabug commented Feb 28, 2009

@DRMacIver said:
Ok. Thanks.

I find it rather surprising (I found it during a presentation while explaining abstract type members, and both Miles and I were baffled), but will assume there's a good reason for it. :-)

@scabug
Copy link
Author

scabug commented Feb 28, 2009

@milessabin said:
Good reason or not, there's some very strange behaviour here,

Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Server VM, Java 1.6.0_10).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class C { type T } 
defined class C

scala> object O extends C
defined module O

scala> var v1 : O.T = _
v1: O.T = null

scala> val v2 : Any = v1
v2: Any = null

scala> val v3 : AnyVal = v1
<console>:7: error: type mismatch;
 found   : O.T
 required: AnyVal
       val v3 : AnyVal = v
                         ^

scala> val v4 : AnyRef = v1
<console>:7: error: type mismatch;
 found   : O.T
 required: AnyRef
Note that implicit conversions are not applicable because they are ambiguous:
 both method any2stringadd in object Predef of type (Any)scala.runtime.StringAdd
 and method any2ArrowAssoc in object Predef of type [A](A)ArrowAssoc[A]
 are possible conversion functions from O.T to AnyRef
       val v4 : AnyRef = v
                         ^

So T is <: of Any, but not of AnyVal or AnyRef, even though null inhabits T?

@scabug
Copy link
Author

scabug commented Feb 28, 2009

@odersky said:
So T is <: of Any, but not of AnyVal? or AnyRef?

yes.

even though null inhabits
T?

Null inhabits T only because you sneakily forgot to initialize the
value. You won't be able to assign null to a variable of type T (try
it!). It's the best one can do with Java `default' initializers,
except for an explicit initialization check. That would probably be
best, but it's not yet implemented.

@scabug
Copy link
Author

scabug commented Mar 5, 2009

@ingoem said:
I leave this open until the spec is updated and reassign to Martin then.

@scabug
Copy link
Author

scabug commented Mar 15, 2009

@odersky said:
I updated the spec.

@scabug scabug closed this as completed May 18, 2011
@scabug
Copy link
Author

scabug commented Jan 31, 2014

@Blaisorblade said:
While looking at #8217, I could actually find the rule in the spec in 5.1: "If the template forms part of a concrete class definition, stats may still contain declarations of abstract type members, but not of abstract term members."

However, this does not describe why it is forbidden to have:

object Baz { type T }

See #8217 for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants