3 ms·
> In this JEP, nullness markers are explicit: to express a null-restricted or nullable type, the ! or ? symbol must appear in source. In the future, it may be u
by mmaniac 2y ago
> In this JEP, nullness markers are explicit: to express a null-restricted or nullable type, the ! or ? symbol must appear in source. In the future, it may be useful, for example, for a programmer to somehow express that every type in a class or compilation unit is to be interpreted as null-restricted, unless a ? symbol is present. The details of any such a feature will be explored separately.
I think this question needs to be answered now. The addition of ? is useless and confusing unless this is planned to eventually happen.
I am inclined not to introduce breaking changes and to only have !.
- MBCook 2y agoIt’s not. The fact that it’s got a ? means a developer explicitly marked it and told you it’s nullable. The lack of a mark means you have to consider it nullable but no one has told you that setting it to null is the right thing. Things may still go wrong if you set it null though. It’s just the contract isn’t explicit in the code.
- jeltz 2y agoNot it is not. They mean different things. String? a = null; String b = null; String? c = ""; String d = ""; String! aa = a; // Compilation error String! bb = b; // Runtime error String! cc = c; // Compilation error String! dd = d; // Works!
- mmaniac 2y agoThanks for clarifying the distinction - that's worse than I thought. When an expression as simple as bb = b can throw something has definitely gone wrong.
- kaba0 2y agoIt has nothing to do with the expression’s simplicity. There are rules uphold by the compiler, and those are displayed in the easiest example possible. There may be additional rules that could show a warning in these cases (e.g. java can track whether a variable gets initialized in both branches and similar quite well), but that’s besides the point. Take these examples in a way that the only thing you know about variable `a` is its type.
- mmaniac 2y agoThese are stupid rules. The point of the proposal is to invite discussion on the proposed rules. Introducing implicit type conversions that throw to a language which is otherwise strongly typed is an awful idea. Java should take a look at its own history and how it was able to retrofit generics into the language. It's nearly all type safe and the unfortunate unsafe parts generate compiler warnings.
- kaba0 2y agoHow is it not type or null safe? It’s as safe as it can be.
- jeltz 2y agoAnd that is why you want to migrate everything to T? and T!.