-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
API Design Review: Varargs #686
Comments
Maybe we should only keep |
Java 8 streams use of(T t) and of(T... values). Not sure if we mustopen the can of worms of varargs, but the Oracle folks don't seem to shy away from it. |
The SafeVarargs annotation is on their side. |
The |
It looks like we have 3 choices:
|
If these are the only options, I'd vote for 3, purely because I generally dislike approach 1 (violates the zero-one-infinity rule [1]) and because 2 adds nothing over 3 except verbosity. [1] http://www.catb.org/jargon/html/Z/Zero-One-Infinity-Rule.html |
I too vote for 3 as it makes the API far cleaner (especially if we end up needing to provide |
@samuelgruetter @akarnokd @zsxwing What are your opinions on this matter? |
Use the builder pattern instead of varargs. Observable.<TCommon>fromBuilder().add(v1).add(v2).add(v3).build().map().etc. |
I'd rather keep what we have right now than that. Observable.from(v1, v2, v3).map() |
Two more ideas:
Observable.<TCommon>just(v1).add(v2).add(v3).map().etc. This is similar to @akarnokd's idea above, but a bit less verbose, and a bit less efficient. And if v1, v2, ... all have the same type, we can even omit the Observable.from(v1, v2, v3).add(v4).add(v5).map().etc.
public final static <T> Observable<T> from(T t1)
public final static <T> Observable<T> from(T t1, T t2)
public final static <T> Observable<T> from(T t1, T t2, T t3)
public final static <T> Observable<T> from(T t1, T t2, T t3, T t4, T... ts) This covers everything except cases with 4 or more arguments of generic type, which should be quite rare. |
By the time you are at such high arity it is easier to put the values in an array and convert that. This kind of code is only used in small samples. (and so far we have been able to keep the builder pattern out,let's keep it that way). |
So let's go for option 3. |
So @headinthebox you think we should go completely vararg and just deal with the warnings that drove us to the current code? Or that we should keep the 1, 2 and 3 arity overloads and then 4+ is with varargs? |
By the way, this isn't just for |
Yes. |
Also go completely vararg for concat? In concat, the arguments are always generic, so users will always get a warning (see #359). |
Annoying yes, but the Java type system is a freaking mess anyway (and just a mirage because of erasure). BTW, can we put @SuppressWarnings("unchecked") in the library source to suppress warnings for users? |
No, the SuppressWarnings is for the call site, not the function definition. The SafeVarargs annotation is a Java 7 feature for the function definition, and it works for simple types like |
My comment above at #686 (comment) shows when I found out that SafeVarargs wouldn't solve the issue. |
What a mess. So the alternative is many mechanical overloads with separate type parameters |
The alternative is what we have right now ... manually creating all of the overloads. It makes it so it doesn't give warnings, but it means we have lots of overloads and painful autocomplete in IDEs. So we either have warnings in the IDE, or autocomplete mess. We have to pick a poison it seems. I don't know which is worse, it's probably a personal opinion. |
Agreed. I guess many people treat "warnings as errors" we should probably go for autocomplete mess. It is unfortunate that there (seems to be) no way in the Java IDEs to control autocomplete. In Visual Studio you can decorate your API to control what pops up. |
I'm going to close this out and choose to leave the code as is, opting to avoid warnings and leave the autocomplete mess. |
There is dislike of the multiple arities of
from
... especially how the 10-arity option always comes up and autocompletes first.For history, changes from vararg to multiple arity overloads was done here: #361 and #359
An alternative is just accept
from(Iterable<T>)
and people call it asfrom(Arrays.asList(1, 2, 3, 4, 5))
which doesn't give the vararg warnings, but this is verbose.On the flip side, the arities from 4-10 seem unnecessary and for "demo code" only.
Perhaps we deprecate the
from(T, T, T)
and higher arities? For higher we would use thefrom(Iterable<T>)
solution?Are there better ways?
/cc @headinthebox and @jhusain
NOTE: This affects
from
,merge
,mergeDelayError
andconcat
.The text was updated successfully, but these errors were encountered: