[antlr-interest] Value types - can't be set to null in C#
antlr at mirality.co.nz
Mon Jan 7 17:01:29 PST 2008
At 13:50 8/01/2008, Terence Parr wrote:
>I'd rather not add syntax for this. If it's not a built-in
>all defaults can be null for objects, right?
Objects, yes. Types, no. In C# you can define "value types" (and
there are a number of standard ones like DateTime) which cannot be
assigned null. On the upside, they are required to have a default
constructor, so if you know that a given type is a value type you
can just do "Type x = new Type();"
Of course from a Java-based code-generation standpoint you have no
way of knowing whether a given type is a value type or a reference
type, which complicates matters. So if the 'default' keyword
thing I suggested a moment ago doesn't work (or if you don't want
to restrict yourself to C# 2.0) then you'll have to have some kind
of syntax to tell ANTLR whether types are value or reference.
(Or alternatively declare them as member variables
somewhere. Member variables get automagically assigned either 0,
null, or default instance as appropriate when the instance is
constructed, unlike local variables. Of course doing this would
either break recursive rules or be a major pain in the butt [and
hurt performance], so I don't think we want to go there.)
More information about the antlr-interest