Re: null-specific argument type?
MG <mgbiz-yvYIh6MZAuFWk0Htik3J/[email protected]> Sat, 1 Feb 2025 18:07:57 +0100
| Newsgroups | gmane.comp.lang.groovy.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Paul,
1. I suggested Groovy could support additional null-like types, which
are also compatible with any type, but can have different semantics
from null.
1. Some examples:
1. nil, representing "nothing" could dissappear when added to a
collection or inserted into a GString.
2. empty could indicate that a variable/field has intentionally
been left uninitilaized (contrary to having been left
uninitialized by error, as null would indicate).
3. default could be a special case for empty, implying that
code should treat this as having some sort of default value
or exhibit default behavior.
4. sql_null could give SQL NULL like logic behavior.
5. error/invalid would represent a generic "error object".
2. This behavior would be impossible to add to static Groovy
without JVM support.
1. In this case the memory adresses 0 = null, 1 = nil, 2 =
empty, 3 = default, ... could be used to encode this.
3. But could be added to dynamic Groovy through NilObject,
EmptyObject, etc.
2. I also asked something along the lineof whether Groovy could support
giving the full call chain in a.b.c.d in the NPE if e.g. c ===
NullObject.
Cheers,
mg
On 31/01/2025 21:54, Paul King wrote:
> We have spoken about adding support (in dynamic Groovy) for NullObject
> just like you suggest. I couldn't find it right now in a Jira or the
> current mailing lists. Maybe it was codehaus days.
>
> The original thinking of NullObject was that it shouldn't normally
> appear in user code. But maybe it's fine in this case.
>
> Paul.
>
> On Fri, Jan 31, 2025 at 7:49 [email protected] <[email protected]> wrote:
>> Hi there,
>>
>> is there a way to write a method with argument which would match null specifically when called? This does not work:
>>
>> ===
>> 1022 ocs /tmp> <q.groovy
>> class Test {
>> def foo(Collection foo) {}
>> def foo(Map foo) {}
>> def foo(org.codehaus.groovy.runtime.NullObject foo) {}
>> }
>> new Test().foo(null)
>> 1023 ocs /tmp> /usr/local/groovy-4.0.25/bin/groovy q
>> Caught: groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method Test#foo.
>> Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
>> [interface java.util.Collection]
>> [interface java.util.Map]
>> ===
>>
>> Thanks!
>> OC