You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Konstantin Smirnov (Jira)" <ji...@apache.org> on 2019/12/20 14:16:00 UTC
[jira] [Updated] (GROOVY-9348) Introduce finer control for AS
keyword with maps
[ https://issues.apache.org/jira/browse/GROOVY-9348?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Konstantin Smirnov updated GROOVY-9348:
---------------------------------------
Docs Text:
Given the following classes:
class A {
B b
int data
int data2
}
class B {
C c
String data
}
class C {
Date data
}
The code works fine:
Date now = new Date()
def a = [ data:42, data2:84, b:[ data:'BBB', c:[ data:now ] ] ] as A
assert a.b.c.data == now
assert a.data == 42
assert a.data2 == 84
Now if I omit the data2:84, the code still works fine except for the last assert of course.
BUT! If I "misspell" the property name like:
def a = [ data:42, data22:84, b:[ data:'BBB', c:[ data:now ] ] ] as A
The 1st assert fails with NPE:
java.lang.NullPointerException: Cannot get property 'c' on null object
and what I see is that only the A class gets instantiated with a no-arg constructor, and b and c properties are all nulls.
Options wich would make the behaviour more persistent:
* do hard-stop: throw an exception in case of unknown property
* do the best-effort cast, i.e. skip the unknown properties only, and fill-in the good ones
* provide a control mechanism to switch between the current "silent" behaviour, maybe by introducing new keywords *asStrict* and *asLenient*
The case is discussed in https://stackoverflow.com/questions/59423991/groovy-as-keyword-for-map-class-binding
was:
Given the following classes:
class A {
B b
int data
int data2
}
class B {
C c
String data
}
class C {
Date data
}
The code works fine:
Date now = new Date()
def a = [ data:42, data2:84, b:[ data:'BBB', c:[ data:now ] ] ] as A
assert a.b.c.data == now
assert a.data == 42
assert a.data2 == 84
Now if I omit the data2:84, the code still works fine except for the last assert of course.
BUT! If I "misspell" the property name like:
def a = [ data:42, data22:84, b:[ data:'BBB', c:[ data:now ] ] ] as A
The 1st assert fails with NPE:
java.lang.NullPointerException: Cannot get property 'c' on null object
and what I see is that only the A class gets instantiated with a no-arg constructor, and b and c properties are all nulls.
Options wich would make the behaviour more persistent:
* do hard-stop: throw an exception in case of unknown property
* do the best-effort cast, i.e. skip the unknown properties only, and fill-in the good ones
* provide a control mechanism to switch between the current "silent" behaviour, maybe by introducing new keywords *asStrict* and *asLenient*
> Introduce finer control for AS keyword with maps
> ------------------------------------------------
>
> Key: GROOVY-9348
> URL: https://issues.apache.org/jira/browse/GROOVY-9348
> Project: Groovy
> Issue Type: Improvement
> Components: Compiler, Static compilation, Static Type Checker
> Affects Versions: 2.5.8
> Reporter: Konstantin Smirnov
> Priority: Major
> Labels: cast
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)