Separate discussion of a topic raised in
Non Nullable Reference types discussion thread
In some of the code examples the type
appears after the identifier
this is so the concept or idea is more prominent rather than the actual syntax used. As this is subject the programming language used.
My default goto programming language to do "back of the paper" coding is vb.net, but I can code in other languages and styles. I learnt programming in 1980s and the language available (to me) at the time where predominantly in the
followed by type
So using C style languages (type identifier
) can break my flow of consciousness, to correct syntax and grammar error. Exception is Nemerle which uses
Overridable Default Values For Null?
With the proposed Non-Null reference types,
is in essence an illegal value for a reference. So I propose that you should be able to define a "default" overload for
in non-null reference type. If
is assign to a non-null (
) the overload instance is used instead.
Type Bang vs Nonnull prefix
A postfix identifier !
after the name of the class
Public Class Foo!
Could be easily missed when scanning through code. In the vb.net example it doesn't look very vb-like. I think this style is OK for type signature of methods and variables though.
A prefix to the class declaration would indicate the the class in non-null.
Public NonNull Class Foo
Using a word instead makes the intent clearer when scanning.
In both example it doesn't look to bad in term of the style.
Default Parameter-Less Constructor ?
Should a non-null be required to supply a parameter less constructor.
nonnull class Example
Example() /* explicitly required */
Doesn't convey that is used in the case of assigning null.
Object base object modified
Extend or modify to base of all .net objects object
to have an overridable method. That is called when assigning null to a non-null.
overridable Object! Default()
/* null for class types */
Special cased in the compiler(s) to allow null
for the base objects of class and structure.
When the default
is subsequently overloaded that return valued must be a
override default Person! ()
return New Person();
Person! p0 = null ; /* --> Person! = Person!.default */
Person! p1 = default<person!> ; /* Person!.default */
This would result in a new instance of Person!
being created for each
The null default overload could be a shared / static instance.
static private Person! _defaultInstance = New Person
override default Person! ()
Person! p0 = null ; /* --> Person!.default --> Person!._defaultInstance_ */;
Person! p1 = default<person!> ; /* Person!.default --> Person!._defaultInstance_ */
This permits the class not to have a default constructor.
If the non-null class is used as a base then it would be possible to return an instance that inherits / derives from that type. A Null Object as in the
Null Object Pattern
base nonnull MyObj
overrides default () MyObject!
return New NullObject
nonnull class NullObject
What if the type is sealed / not inheritable and you need a non-null version?
Suppose we allow extension methods.
static class Null_Exts
static string! default()
You could the localise nonnull - ability on a file level.
You could also potential return a non-null (0 in most cases) for the default value of a value (structure) type.
Optional Non-Null Parameters
What about in the case of optional parameters where the parameter types is non null?
public method ( arg : T! = null )
public method ( arg : T! = default )
/* args has the value of T!.default in both examples*/
Another option is to allow an arbitrary expression so longs as it return a
public method ( arg : T! = expr< T! > ) ./^
Option Null_Coalesce False / True
option Null_Coalesce False
Assigning null to a non-null will result in null reference exception
option Null_Coalesce True
Assigning null to a non-null will result in the null
being coalesced to the default null overload value. If this has not been overloaded then some form for exception is thrown.