3 ms·
> More generally than just Java, nullability is often a property not of a type but of a variable. This is a tangent, but I'm not sure I follow this. Can you gi
by zoogeny 4mo ago
> More generally than just Java, nullability is often a property not of a type but of a variable.
This is a tangent, but I'm not sure I follow this. Can you give an example to make this clear?
- pron 4mo agoYes, but it comes from Java having both runtime and compile-time types; it's harder to make the distinction in languages that don't have runtime types. In Java, you can ask, `x instanceof T` (and this is a runtime test), which means, is x one of the values in the set of values allowed by T. `null instanceof Integer` is false, even though a variable of type `Integer` can be assigned a null. So you can think of `Integer x` as being `Integer|null x`, i.e. x can hold a null, even though `null instancof Integer` is false.
- zoogeny 4mo agoI think I mostly got this, but just to test it, it would be like in Typescript where I might say: type Foo = { x: number; } type Bar = { x: number; y: number } type FooBar = Foo | Bar; function baz(x: FooBar) { if ('y' in x) { // compiler now knows x is a Bar } } In this case, the variable `x` has a property that is determined by the compiler based on control flow. i.e. it isn't explicitly carried by the type of `x`.