-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
RFC: re-opening the Into<Option<_>>
debate :)
#805
Comments
I think both of these parts are problematic. The signature is probably an obstacle for new users, as is the requirement to do something special for the Also for objects we have this problem anyway regardless of any of this because of the I'm wondering if for strings it wouldn't actually be better to use For functions and closures we currently always use boxed dyn closures when an Usually a better approach for improving these APIs is to provide a function with and without the optional closure argument. Just some initial thoughts so far. |
The string discussion also has connections to what is planned in #600 (comment) FWIW |
That's true, but with Rust expressive type system, "powerful" models can result difficult to understand to newcomers anyway. Luckily, we also have a powerful doc tooling (wink wink @GuillaumeGomez). With proper documentation, users should be able to figure out what can be done. Documentation shows up in the |
I gave this some more thoughts and made some changes to my experiment. This is also inspired by @sdroege's introduction of The resulting implementation makes no more uses of any
|
As we have However we should also impl @bilelmoussaoui Any opinions here? |
This way strings can be passed to such functions without explicit `Option` wrapping, see gtk-rs#805.
See #891 |
As mentioned previously, it will increase the gtk-rs code complexity and also make documentation harder to read. Is the gain really worth it? |
@GuillaumeGomez did you reply to the initial comment or to the refined: #805 (comment) @slomo I'll try to take a look in the coming days. As I said in the comment I refered to above, I believe explicit methods to discard, unset, etc. would be valuable IMO. |
I was answering to @sdroege based on your original arguments and small tests I ran. Based on your refined comment, there is no more use of |
Update
My views on this topic have evolved, see the comment for an up to date version: #805 (comment)
Following is the initial obsolete comment for the record.
Another
Into<Option<_>>
experimentObsolete: see update in #805 (comment)
In
gstreamer-rs
a fix for nullability inconsistency had an API change induce an update to all the call sites for a function fromq.set_uri(uri)
toq.set_uri(Some(uri))
. This revived the debate whetherInto<Option<_>>
should be used in argument position when the argument is optional.This debate was previously concluded: "Into<Option<_>> considered harmful". The main arguments leading to this conclusion are:
None
.We wanted to figure out whether the problems encountered at the time should still be considered harmefull and whether
Into<Option<_>>
should be reintroduce, at least in some cases, as we ended up doing forset_uri
.TLDR
The TLDR is the above arguments are still valid:
None
. We probably don't want to use this with closures or complex function arguments.q.set_uri(uri)
, they will probably not guess they can useq.set_uri(None)
too.Nevertheless, code looks nicer IMO when at least some of these arguments use this feature. I conducted an experiment on
gstreamer-rs
changing most candidates in manual code and updatedgst-plugins-rs
to use the resulting code base. See the difference in thisgst-plugins-rs
commit.In this issue, I'd like to show the result of a quick experimentation on
gtk-rs-core/glib
to illustrate the pros & cons. The changes and some illustrative test cases are available in thisgtk-rs-core/glib
commit.The easy cases
There are easy cases for which there's no issue apart from the users not immediately figuring out that the arguments can be an
Option
.Copy + Clone
typesThe
Copy + Clone
types handling is straightforward with limited impact on the function signature and body.See the
Channel::new
implementation and the above test cases.Reference to concrete types
When the argument's inner type is a reference to a concrete type, the implementation is quite straightforward too, with the addition of a lifetime.
See the
DateTime::from_iso8601
implementation and the above test cases.The less easy cases
Reference to a type by one of its trait implementation
This is where it starts to get tricky.
Strings
One very common case for this is when the argument is a string, like in
set_uri
. Currently, most functions use anOption<&str>
for these kind of arguments. This allows using:With the changes in
ParamSpecBuilderExt::set_nick
, besides the sameSome
variants, we get:The
None
case is unfortunate. It shows up here, but it actually was already an issue for others use cases which lead to the introduction of theNONE
constant for some types.The signature is a bit ugly, the
?Sized
bound is necessary to accept thestr
literal.I tried to have the signature accept plain
String
, but I gave up.IMO the type annotation annoyance is acceptable compared to the usability improvement. There are many of these in this
gst-plugins-rs
commit.Subclasses
Another common use case involves subclasses. See
SignalGroup::set_target
as an example. In this case, with current API, we need to use theNONE
constant:Disambiguation is still necessary with
Into<Option<_>>
, though we can use this instead:Which could render the
NONE
constants unnecessary. Note that this could be possible with current API if the type was declared as a generic argument<T: IsA<_>>
instead of animpl IsA<_>
.IMO, when dealing with subclasses this is useful as it leads to leaner code. It might not be applicable to all functions though.
Functions and closures
The last example combines two types from their traits. One is a
Path
, so it is quite similar to thestr
case and the other is a function trait. This can be seen in thespawn_async
implementation.Of course, there's nothing special about the plain and
Some
cases. For theNone
case, we get to provide type annotation for theNone
argument:For some reason I can't immediately explain, using a
NONE
constant doesn't work:This is getting tricky. The function signature is mimimalist here, but it would become unacceptable to impose a full signature to users who only want to pass
None
is the first place.Conclusion
IMO we should use
Into<Option<_>>
on a case by case basis:gstreamer-rs
, we started discussing cases where we may not want to do it.Apart from the manual code, applying this to automatically generated code could be useful, so a change to
gir
would be necessary.The text was updated successfully, but these errors were encountered: