-
Notifications
You must be signed in to change notification settings - Fork 431
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
first pr of Groovy-Optional #420
base: master
Are you sure you want to change the base?
Conversation
The dsadvantage of using missingMehod is of course, that any method defined on Optional cannot be called that way. I am worried about map, flatMap, filter, toString, equals |
Thanks your comment.
those behavior is exactly same as Java so upper compatibility is kept perfectly. But in the case of toString() /equals() / hashCode() might be regarded as a problem. So I think we have no choice to override the meaning of Optional's methods |
what I did mean was more something like this:
will return a Stream of Long with 2 as single element,
will not work at all, because it would compare the number 1 with a Stream of Longs. It looks a bit constructed with a Stream, but it is to be expected that there are many more tpyes that support that in the future. What I am saying is that wrapping a value in an Optional and then have the wrapper act transparently lke the object inside is not going to work. And even if we fix hashcode and equals for us, the moment you place it in a collection all bets are off again. I won't say this pull request is a bad idea, not at all! But first we should clarify what the target of this is and the see what we need to get there |
I get the point. Optional is a idea comes from Scala or some FP area(haskell, ocaml,..) The problem of Optional is, to get the value from Optional,
if you have 2 values, and wrap up to Optional after operation, the code would be like:
This is almost as bad as null handling.
but this still verbose, Especially in Groovy, because we have safe navigation already.
In Scala, for-comprehension (monad comprehension) make more simple, but Groovy don't have now. To sum up, there is no good enough language support in Java to extract Optional value and operate it and wrap-up again after operation. By this proposal we can write simple-and intuitive way.
But this works just under restriction which your pointed. As another idea, how about expand the meaning of "?. " on Optional?
|
sorry, for the really long delay... using "?." is interesting, but your original problem was
and there it won't help. |
thank you comment again. You are right. I think ?. is converted depends on weather the type of method parameter is Optional or not.
without STC, dynamic type checking with instanceof call is required.
In addition, null check should be done in actual code generation. |
but you see that you cannot write lastName + " " + firstName to get this result? It is because there is no way to express a "?." for operators. It is already the same for safe null navigation, but you while you use null-safe navigation only in a few places normally, you are forced to use optionals all the time, once you start using them. In that optionals are a very intrusive element. That starts for me the question if a syntax element you have to use all the time for this is the right thing. Instead maybe it should go deeper and become part of Groovy's internals. This idea goes a lot more in the direction of your initial idea. But what I have in mind is much more complex. Instead of having some kind of helper type to do dynamic dispatch with and being lost in static compilation, How about making optional a "wrapper type". That means if the runtime detects an optional, it reacts internally different to it having a value present or not. Then a+b could be transparently done with optionals. To give an example of what I am thinking here... if you make a method call on receiver, then the runtime will check if the receiver is null, and act different according to that. Of course Optional would be done slightly different... and potentially a lot of places have to change for this...but this is where I see the maximum use... Do you agree? |
I commented on this and then accidentally deleted it, so I will try again. @uehaj You want to transform a sequence of safe navigation to a sequence of
assumes lastName is optional, but firstName is mandatory. Let's assume lastName is also optional and the space is mandatory. The code is then:
or if " " is optional then:
I wrote a dynamically typed method to do this sequence of calls using missingMethod calls so you can write:
I intended to rewrite using a macro, but have not gotten around to it. With a macro, this could be typechecked statically. Without a macro (for static typing) you need to ensure the values are monads (so they have flatMap and map) or can be transformed to monads. Scala and Haskell do this using implicits and typeclasses. @paulk-asert has made progress towards implicit conversion for assignment statements. It would be good if this could be extended to general expressions. |
Hi brackdrag, I guess your "wrapper type" of Optional handling is done under the similar way where primitive types handling in Groovy isn't it? Java's primitive types are converted to Boxed type versa-visa at run time. But it could be hard work. Not only around Java method call, you have to wrap/retrieve operation at all of the retrieve/store operation for the containers which have Optional value, like List() or array of Optional, .etc. |
Hi, mperry, thanks for comment. http://uehaj.hatenablog.com/entry/2014/12/24/030136 best regards, |
@uehaj I found the auto translation very hard to read. I would be interested in your explanation of the custom type checker. |
Hi @mperry I wrote document: https://github.com/uehaj/groovyz if you prefer |
FYI: https://github.com/rust-lang/rfcs/blob/master/text/0517-io-os-reform.md#the-error-chaining-pattern |
Last year I proposed a way of transparent handling of java.util.Optional.
http://groovy.329449.n5.nabble.com/Groovier-way-of-handling-Java8-s-java-util-Optional-td5717544.html#a5717561
https://github.com/uehaj/groovy-optional
I think this is very groovy way to handle Optional value, although Optional itself might not be good idea compared to safe navigation, anyway.
I imagine you groovy development team are tackling Java8 aware GDK methods?
So I rewrite above proof of concept code to actual PR to current Groovy
implemented as a GDK method java.util.Optional.methodMissing().
I hope reviewing this, and please advice me about anything to do.
(for example where is the right branch to rebase?)
Probably it is needed to add custom type checking rule to avoid STC errors
which comes from use of Optional.methodMissing.
Best regards,