But the type checker is able to prevent null from being assigned in the same cases. And it is able to prevent a nullable value from being assigned to a scalar variable (not array/dictionary member) which is typed to be non-nullable.
- SDK 8.1.1
- type check level: 1, 2 or 3
import Toybox.Lang; var floatVal as Float = 42.0; var floatArray as Array<Float> = [ 42.0 ]; var floatDictionary as Dictionary<String, Float> = {} as Dictionary<String, Float>; function bar(x as Float?) as Void { floatVal = null; // ✅ ERROR: fr955: Cannot assign value 'Null' to member ':floatVal'. floatVal = x; // ✅ ERROR: fr955: Assigned value type 'PolyType<Null or $.Toybox.Lang.Float>' to member '$.floatVal' of non-poly type '$.Toybox.Lang.Float'. floatArray[0] = null; // ✅ ERROR: fr955: Cannot assign type 'Null' to '$.Toybox.Lang.Array<$.Toybox.Lang.Float>'. floatArray[0] = x; // ❌ no error floatDictionary["foo"] = null; // ✅ ERROR: fr955: Cannot assign type 'Null' to '$.Toybox.Lang.Dictionary<$.Toybox.Lang.String,$.Toybox.Lang.Float>'. floatDictionary["foo"] = x; // ❌ no error }
Expected behaviour:
- every line in bar() should have an error related to assigning a null / nullable value to something which isn't nullable
Notes:
- the type checker clearly knows that x is a Float or Null as per the error when trying to assign it to floatVal, and as per the type information that appears when you hover over any instance of x:
> var x as $.Toybox.Lang.Float or Null
- as per the comment below, the general issue seems to be that the checker only ensures that at least one type of a given polytype is valid when it's assigned to an element of an array/dictionary, while it should check that all types are valid.