-
Notifications
You must be signed in to change notification settings - Fork 553
Model nullability with Option<T> #331
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
For reference, in winrt-rust we also made the same change after a while: contextfree/winrt-rust#54 Probably, many methods never actually return |
By the way, in winrt-rust we also added an exception for the |
Thanks Patrick, double unwrap is certainly a bit gross. Yes, .NET has the NotNull attribute. We could introduce such an attribute for WinRT, but the challenge is always getting any adoption for such an attribute on the many existing APIs. I do like the idea of excluding certain common patterns, like constructors and async. |
Here's the attribute proposal: microsoft/xlang#708 |
Looks like the attribute may not be practical, but I think we can grow the number of return types that are, heuristically, not-null to the following:
|
#354 fixes this both in modeling nullability with |
Right now any WinRT interface/class/delegate types (reference types) can store a null pointer value. We should change that so that a null pointer value is not a valid state and the developer has to use
Option<T>
to express that. While this is more syntactically verbose in some cases, at least when you hold aT
you know it's valid and can use it directly with no runtime or syntactic overhead. WinRT class constructors (T::new()
in Rust) will simply returnResult<T>
but all other return/out params of reference types will simply returnResult<Option<T>>
on the assumption that it could be null. This avoids the confusion around whether a given value may be used or whether the is_null check should be performed, which is not idiomatic in Rust.Beyond constructors, we should be able to avoid the extra
Option<T>
for activatable factory interfaces since they are exclusively used to implement constructors.The text was updated successfully, but these errors were encountered: