elena-lang icon indicating copy to clipboard operation
elena-lang copied to clipboard

Nullable support

Open arakov opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe. Compiler should warn if the operation with nil is possible. Nil-safe code should be used instead. Only implicitly nullable types should allow operation where the target can be nil

Describe the solution you'd like The type declaration should have a special attribute ?

import extensions;

singleton Tester
{
   test(int? prm)
   {
      ^ prm.hasValue ? (prm.Value) : 0
   }
}

public program()
{
   var t := Tester;

   console.printLine("Tester.test(2)=",Tester.test(2));
   console.printLine("t.test(3)=",t.test(3));

   console.printLine("Tester.test(nil)=",Tester.test(nil));
   console.printLine("t.test(nil)=",t.test(nil));
}   

The nullable structures are special case - they are in fact a reference type

Open issues:

  • boxing to Nullable<T>?
  • support nullable reference (when option is on, warning on unsafe operation)
  • nullable : int? == int? -> return bool?
  • nullable : int? + int? -> return int? (if both nil -> the result is nil)
  • nillable argument list

arakov avatar Jan 20 '23 09:01 arakov

  • Nullable<T>.ValueOrDefault to returen value or default value
  • nullable template argument
  • nullable closure argument

arakov avatar Jan 24 '24 12:01 arakov