groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dinko Srkoč <dinko.sr...@gmail.com>
Subject Re: CompileStatic and right-hand-side literals
Date Wed, 02 Dec 2015 15:50:12 GMT
On 2 December 2015 at 16:26, Cédric Champeau <cedric.champeau@gmail.com> wrote:
> You are actually talking about the second error, but it's a direct
> consequence of flow typing (that Scala doesn't have). And if we didn't have
> that, no doubt that even more frustration would come (look at the history of
> type checking bugs for type inference of literals to get an idea).

Yes, sorry, the second error.

Anyway, here by reducing the type information, we can actually help
the type checker.
Less is indeed more. :-)

  ([a: 'foo'] as Map) + [b: 1]  // this works

Cheers,
Dinko

>
> 2015-12-02 16:23 GMT+01:00 Dinko Srkoč <dinko.srkoc@gmail.com>:
>>
>> On 2 December 2015 at 15:48, Cédric Champeau <cedric.champeau@gmail.com>
>> wrote:
>> > I think that the type checker is technically correct here. The RHS is
>> > correctly inferred (it's a `Map<String, String>`). And since you are
>> > trying
>> > to assign it to `Map<String, Object>` it fails. It's a consequence of
>> > inference of literals (Java doesn't have such literals so no such
>> > issues).
>> > You have to separate declaration from assignment, or use a cast.
>>
>> It may be technically correct, but it also seems a bit too rigid. If
>> it tried to calculate the LUB of those type parameters, it would infer
>> the type as `Map<String, Object>` and wouldn't report the error.
>>
>> As it happens, I encountered this very problem (from Jason's example,
>> at line: 4, calling `#plus`) today.
>>
>> I reluctantly call upon Scala, but it has a solid type system and it
>> does the right thing here:
>>
>>   scala> Map("a" -> 1) ++ Map("b" -> "foo")
>>   res0: scala.collection.immutable.Map[String,Any] = Map(a -> 1, b -> foo)
>>
>> I'm not saying this is how Groovy must behave, just that current
>> behaviour is a bit surprising. ;-)
>>
>> Cheers,
>> Dinko
>>
>> >
>> > 2015-12-02 15:44 GMT+01:00 Winnebeck, Jason
>> > <Jason.Winnebeck@windstream.com>:
>> >>
>> >> Is there a way to avoid awkward typing issues with literals as in this
>> >> case:
>> >>
>> >> @CompileStatic
>> >> void f() {
>> >>   Map<String, Object> x = [a: '1']
>> >>   println x + [b: 2]
>> >> }
>> >>
>> >> This results in the following errors in 2.4.5:
>> >>
>> >> [Static type checking] - Incompatible generic argument types. Cannot
>> >> assign java.util.LinkedHashMap <java.lang.String, java.lang.String>
to:
>> >> java.util.Map <String, Object>
>> >>  at line: 3, column: 27
>> >>
>> >> [Static type checking] - Cannot call <K,V> java.util.LinkedHashMap
>> >> <java.lang.String, java.lang.String>#plus(java.util.Map
>> >> <java.lang.String,
>> >> java.lang.String>) with arguments [java.util.LinkedHashMap
>> >> <java.lang.String, java.lang.Integer>]
>> >>  at line: 4, column: 13
>> >>
>> >> It seems that the static compiler has issues with inferring RHS types.
>> >> I
>> >> think it might be similar to this case I run into from time to time:
>> >>
>> >> @CompileStatic
>> >> void g(List<Integer> items) {}
>> >>
>> >> @CompileStatic
>> >> void h() {
>> >>   g(Collections.emptyList())
>> >> }
>> >>
>> >> Results in:
>> >> [Static type checking] - Cannot call ConsoleScript6#g(java.util.List
>> >> <java.lang.Integer>) with arguments [java.util.List <T extends
>> >> java.lang.Object>]
>> >>
>> >> Jason Winnebeck
>> >>
>> >> ----------------------------------------------------------------------
>> >> This email message and any attachments are for the sole use of the
>> >> intended recipient(s). Any unauthorized review, use, disclosure or
>> >> distribution is prohibited. If you are not the intended recipient,
>> >> please
>> >> contact the sender by reply email and destroy all copies of the
>> >> original
>> >> message and any attachments.
>> >
>> >
>
>

Mime
View raw message