ceylon / ceylon-spec

DEPRECATED
Apache License 2.0
108 stars 34 forks source link

Can’t use optional types in enumerated bounds of generic type constraints #941

Open lucaswerkmeister opened 10 years ago

lucaswerkmeister commented 10 years ago

The spec says (3.2.8, 1.0, current) that

X? means Null|X for any type X

however, you can’t actually use it in generic type constraints:

void error<Param>() given Param of Object? { } // enumerated bound must be a class or interface type
void works<Param>() given Param of Null|Object { }

I can’t find any mention of this in the section on generic type constraints (3.5.3, 1.0, current).

Is this intentional, or an oversight in the grammar/typechecker?

gavinking commented 10 years ago

Well if you check the definition of CaseTypes in the spec, you'll see that the thing that follows of isn't actually an arbitrary type expression. Rather, it's a list of types, with I as punctuation.

So actually this is intentional.

lucaswerkmeister commented 10 years ago

Okay, but then I guess it’s a bit confusing... “X? means Null|X for any type X” reads to me as if replacing X|Null with X? and vice versa should never have any effect, but in this case it does make a difference.

Actually, looking at the grammar, I don’t understand why this isn’t accepted. caseTypes takes an abbreviatedType, which includes optional types.