-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should we null-short toString() and hashCode? #393
Comments
I agree that we should short-circuit based entirely on syntax, not on the methods used. It's a good idea because it's very confusing if Take Take: Assume some class As for the underlying model: My model for nullable extension methods, and NNBD types in general, is that there is a magical Top type which declares the "Object members" ( |
I totally agree on "keep it simple and syntactic", cf. #394 (comment). There's no need for any magic in the model, that is, in the subtype graph: The top type can be denoted by However, there is one more topic at play here: As part of that eternal quest to empower developers by not requiring them to waste brain cycles on accidental complexity, I think it's important to converge on an interpretation of the null object. I think we have implicitly agreed to interpret null as a marker for absence in many contexts:
These rules are applicable to code which is aimed at an application domain, and they allow us to model absence quite consistently. However, there are situations where the code needs to abstract that distinction away (between being present and being absent). In particular:
We decided (#175) that we allow For the
We do need to support both the application domain level interpretation of null as "absence" (so null gets special treatment), and general/"representational" code that abstracts that distinction away, and the only problem seems to be that I suspect that this ambiguity could be biased toward the application domain, especially for string interpolations. We could then introduce an extra constraint that This doesn't help developers who write WDYT? |
I kind-of like the "interpolation must be non-nullable" rule. It is a "high-level" feature. |
Wouldn't you write: list?.length?.toString() ? Basically, if we decide a method chain stops shorting for whatever reason, you can always opt into more null-awareness by just writing Overall, @lrhn, I'm persuaded about null-shorting. I think a simple syntactic rule that users can easily remember plus an easy syntax to opt out of that behavior when it isn't what they want is a better solution than something that does the right thing more often but is harder for users to know that it does the right thing. I like the principle of requiring non-nullable types in interpolation. It will prevent "null" accidentally appearing in user strings. But my suspicion is that it will be too onerous in practice to do. Maybe we can do an experiment to see how often this would force users to write |
Closing out because I agree the answer should be "yes" so I think we're in agreement now. |
You would probably never need to write |
Currently, we say the rest of a method chain after a null-aware operator is shorted, even calls to extension methods. That's potentially not useful if those extension methods are on nullable types (#392). If we change that to stop shorting on nullable extension methods, then it seems the principle for null-shorting is "only short operations that would fail".
Given that, what does that say about null-shorting
toString()
andhashCode
? Those operations wouldn't fail. Should they still be shorted?My intuition is that we do want to short them because it's likely that a user does not want to call
toString()
on null just because some prior operation happened to return null. Unlike an extension method declared on a nullable type, there's no point where there's a clear intention to allow the operation on things that are null.Anyway, I'm not sure if re-opening the discussion around null-shorting extension methods has implications for these members too. Perhaps they should not be available on nullable types at all? (In other words, since Object and Null each define their own
toString()
we treat the union as not having that member since it isn't the same member.)The text was updated successfully, but these errors were encountered: