Scala Programming Language
  1. Scala Programming Language
  2. SI-1753

Concrete classes can have abstract type members

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Documentation and API
    • Labels:
      None

      Description

      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.

        Activity

        Hide
        Geoffrey Alan Washburn added a comment -

        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.

        Show
        Geoffrey Alan Washburn added a comment - 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.
        Hide
        David R. MacIver added a comment -

        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.

        Show
        David R. MacIver added a comment - 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.
        Hide
        Miles Sabin added a comment -

        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?

        Show
        Miles Sabin added a comment - 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?
        Hide
        Martin Odersky added a comment -

        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.

        Show
        Martin Odersky added a comment - 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.
        Hide
        Ingo Maier added a comment -

        I leave this open until the spec is updated and reassign to Martin then.

        Show
        Ingo Maier added a comment - I leave this open until the spec is updated and reassign to Martin then.
        Hide
        Martin Odersky added a comment -

        I updated the spec.

        Show
        Martin Odersky added a comment - I updated the spec.
        Hide
        Paolo G. Giarrusso added a comment -

        While looking at SI-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 SI-8217 for that.

        Show
        Paolo G. Giarrusso added a comment - While looking at SI-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 SI-8217 for that.

          People

          • Assignee:
            Martin Odersky
            Reporter:
            David R. MacIver
            TracCC:
            Erik Engbrecht
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development