Skip to content
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

Provide a way to deal with null return values #20

Closed
Boddlnagg opened this issue Aug 25, 2016 · 4 comments
Closed

Provide a way to deal with null return values #20

Boddlnagg opened this issue Aug 25, 2016 · 4 comments

Comments

@Boddlnagg
Copy link
Collaborator

Since we can't be sure whether interface return values are never null, we probably have to check that and return an Option<ComPtr<...>> instead. We should try to make use of the null-pointer optimization here, so there will be no unnecessary overhead.

@Boddlnagg
Copy link
Collaborator Author

Boddlnagg commented Sep 3, 2016

This is complicated by the fact that we can't just use Option<ComPtr<...>> in arrays, at least not without the null-pointer optimization. Option<ComPtr<...>> must be pointer-sized for that to work.

@Boddlnagg
Copy link
Collaborator Author

Boddlnagg commented Dec 22, 2017

Alternatively we could change our definition of Result: We could either define a new result type with three variants (Ok, None/Missing and Err) or extend the Error type with a Null variant. The first one would be closer to the actual semantics of WinRT (because there's a difference between returning null and actually throwing an error/exception, but returning null is not really considered an error), but it would mean that we can't just use ::std::result::Result. I don't know if the latter would be a problem (it might be totally okay now that we can use ? with custom types).

This is better than Option<ComPtr<...>> because it doesn't require double-unwrap (or double-?) every time I want to access a method's result.

See https://docs.microsoft.com/en-us/uwp/api/windows.storage.istorageitem2#Windows_Storage_IStorageItem2_GetParentAsync for an example of a method where it is explicitly documented that it can return null.

@Boddlnagg Boddlnagg changed the title Wrap ComPtr<...> return values in Option<> Provide a way to deal with null return values Dec 22, 2017
@Boddlnagg
Copy link
Collaborator Author

Boddlnagg commented Dec 22, 2017

Another thing: null return values can only happen for interface return values (and HStrings, but we already deal correctly with those), i.e. those types that are currently wrapped in ComPtr<...>. But not for primitive types (including ()), structs or enums, obviously. So should we use different Result types depending on the category of the return type?

@Boddlnagg
Copy link
Collaborator Author

An interesting special case might be the async task results. Maybe we can assume that a returned IAsyncOperation<...> is never null?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant