onelang / OneLang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nullability

koczkatamas opened this issue · comments

Nullability in target languages

In some languages any variable can be null, even primitive integer types. This is true for mostly dynamically-typed languages, eg. Ruby, Python, Perl, PHP, JavaScript.

In C# and Java most of the types (eg. String, Array, Map, user-defined types) can be null, but the primitive ones are not (in C# we can use the Nullable class / ? qualifier for that, in Java the boxed instances: eg Integer instead of int).

In the languages mentioned above there is no option the make the nullable types non-nullable.

In other languages nothing can be null by default, only if we make them nullable explicitly (eg. by using pointers in C++ and Go or the ? qualifier in Swift).

In TypeScript the default nullability depends on the strictNullChecks configuration.

Nullability in OneLang

The current implementation has a pretty adhoc nature in the nullability area. Generally primitive types (bool, int, float), string and enums are non-nullable, while everything else (Array, Map, user-defined types) is nullable.

At first string was nullable, but this leaded to some pretty ugly code eg. in C++ at string concatenation.

Nullability of Array and Map is still a question. null is a two-edged sword, it can be used as an easy way to indicate missing value, but allowing null makes the code more error-prone and can generate ugly code in languages which are non-null by default.

The best way would be a more clever compiler which can decide if a field / variable can be ever null, but this is not the goal of the PoC milestone, so we currently allow everything to be null, until this causes serious problems (even if it results in some really ugly generated code).