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

Question: Singleton Seq of Array #89

Closed
danieldietrich opened this issue Apr 21, 2015 · 11 comments
Closed

Question: Singleton Seq of Array #89

danieldietrich opened this issue Apr 21, 2015 · 11 comments

Comments

@danieldietrich
Copy link

Hi Lukas,

I'm not convinced about the java.util.stream.Stream.of() API. There are

  • Stream.of(T t) respectively Seq.of(T t)
  • Stream.of(T... values) respectively Seq.of(T... values)

which does not allow us to create a Stream/Seq of an Array, i.e.

  • Seq.of(new String[] { "hello" }) is a Seq<String> instead of a Seq<String[]>

Workaround:

  • Seq.of(new String[][]{{ "" }}), which is ugly

Just want to hear your opinion about the practical value of having a special API for a singleton Array Seq.

And also: What is the use case for a Stream.of(T t). If there would be only a Stream.of(T... values) we could also call Stream.of(t)!? Looking at the implementation of java.util.stream.Stream.of(T t) I see a difference, but from the API perspective I don't see the point.

- Daniel

@lukaseder
Copy link
Member

Just want to hear your opinion about the practical value of having a special API for a singleton Array Seq.

It's simple. The use-case of Seq.of(1, 2, 3, 4, 5) is just more useful and common than the use-case of Seq.of(new String[][] {{ "" }}). So, this API is a compromise worth making, although I see your point. Another workaround:

Seq.of((Object) new String[] { "" });

Looking at the implementation of java.util.stream.Stream.of(T t) I see a difference, but from the API perspective I don't see the point.

Less pressure on the GC due to unneeded array references? EnumSet.of(...) took this even further

@danieldietrich
Copy link
Author

Thanks, I thought I've overseen s.th.

I like varargs, too. A little bit nasty is the need for @Safeargs and @SuppressWarnings("unchecked") in general (for API designers).

An alternative for creating a singleton would be another method name, like Seq.singleton(String[] { "" }). However, Seq reflects the java Stream API and I think here is nothing to do.

My question is clarified, the issue can be closed :-)

Thanks again!

  • Daniel

@lukaseder
Copy link
Member

A little bit nasty is the need for @SafeVarargs

You're telling me? I'm maintaining a huge Java-6 compatible API (jOOQ). I could kick someone for not having introduced @SafeVarargs any earlier :-) (and in a better way)

An alternative for creating a singleton would be another method name, like Seq.singleton

Yes, that would match the existing java.util.Collections.singleton().

However, Seq reflects the java Stream API and I think here is nothing to do.

Yes. Good enough for "us". You can do better, in Javaslang :-)

@danieldietrich
Copy link
Author

That's fair enough :-)

@lukaseder
Copy link
Member

I've just thought of another reason why you should overload method(T) and method(T...):
http://blog.jooq.org/2014/09/19/learn-how-nashorn-prevents-effective-api-evolution-on-a-new-level/

@danieldietrich
Copy link
Author

Thank you for sharing! That was insightful, there is so much to learn!

Also interesting how deep and thoughtful little things are which seem to be trivial at first sight.

@jbgi
Copy link

jbgi commented Apr 29, 2015

I think the proper work around is
Stream.<String[]> of(new String[] { "hello" });

@lukaseder
Copy link
Member

@jbgi: Yes, you're right

@danieldietrich
Copy link
Author

@jbgi it is 6 chars longer than Stream.of(new String[][]{{"hello"}}); but way less ugly :-)

@danieldietrich
Copy link
Author

Just for the sake of completeness:

And also: What is the use case for a Stream.of(T t). If there would be only a Stream.of(T... values) we could also call Stream.of(t)!? Looking at the implementation of java.util.stream.Stream.of(T t) I see a difference, but from the API perspective I don't see the point.

It's the null - Stream.of(T... values) throws when called with null arg.

@lukaseder
Copy link
Member

It's the null - Stream.of(T... values) throws when called with null arg.

Interesting point, I haven't thought of this... This is probably relevant only when passing a null literal such as of(null), though, which should intuitively correspond to of((Object) null), but of course doesn't, really meaning of((Object[]) null)

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

No branches or pull requests

3 participants