bug icon indicating copy to clipboard operation
bug copied to clipboard

Compiler allows empty argument lists for traits

Open scabug opened this issue 12 years ago • 6 comments

scala> trait T
defined trait T

scala> class C extends T()
defined class C

scala> new T(){}
res0: T = $anon$1@3ffa1b16

scala> class C extends AnyRef with T()
<console>:1: error: traits or objects may not have parameters
       class C extends AnyRef with T()
                                    ^

scabug avatar Dec 12 '12 13:12 scabug

Imported From: https://issues.scala-lang.org/browse/SI-6805?orig=1 Reporter: @xeno-by Affected Versions: 2.9.2, 2.10.0

scabug avatar Dec 12 '12 13:12 scabug

@xeno-by said: This situation happens because in a lot of places in the compiler we silently promote Nil to ListOfNil. Therefore there's code that compensates for such promotions in cases when the receiver is actually nullary.

Hence it's currently impossible to do the validation for New, but as of late it's possible to do the validation for parent types as in extends T() and new T(){}. So the fix should be a real simple change to typedParentType.

On the other hand, do we really want to fix this thing given that we plan to introduce trait parameters in the near future? Martin, could you please comment?

scabug avatar Dec 12 '12 13:12 scabug

@odersky said: I think it should have low priority. No need for a fix.

scabug avatar Dec 12 '12 13:12 scabug

Scala 3 does have trait parameters now

SethTisue avatar Aug 19 '23 19:08 SethTisue

scala 2.13.11> trait T
trait T

scala 2.13.11> class C extends AnyRef with T()
class C

som-snytt avatar Aug 19 '23 23:08 som-snytt

What he meant is that inMixinPosition is now also checked in typedParentType and falls under the fix.

trait T() is able to fail in parser.

som-snytt avatar Mar 31 '25 02:03 som-snytt