Hi guys,

first, congrats on Groovy 6.0.0 G-)

We are still on Groovy 4 (kind of dreading what IntelliJ Intellisense will do with a newer Groovy version, given how much code it incorrectly marks as "erronous" now), and I finally found the time to do a quick spike to create an early-out null-mapping macro, which tests only for !== null (no Groovy truth, because in 99% of cases that is not what we want) and takes an arbitrary number of arguments:

Examples:
final x = NULL_TO(x0(),x1(),x2(),x3(),x4()) // same as ternary/Elvis operator, x1(),etc is only evaluated, if x0() evaluates to null, etc final sqe = NULL_TO(sqlExecutorIn, Global.sqe, createSqlExecutor()) // again, createSqlExecutor() is only called iff sqlExecutorIn and Global.sqe are both null

Macro stub:
static <T> T NULL_TO(T... args) { ... }

This works fine, the only problem is, that under static compilation the resulting return type of NULL_TO always seems to be Object (instead of generic type T), so I have to use an explicit type cast in these cases, which is decidedly un-Groovy.

Examples:

// If x0(), etc all have return type BigDecimal, fails later on when trying to use BigDecimal functionality on x under @CompileStatic without the cast
final x = (BigDecimal) NULL_TO(x0(),x1(),x2(),x3(),x4())

// Fails with "Object cannot be assigned to SqlExecutor" under @CompileStatic otherwise final SqlExecutor sqe = (SqlExecutor) NULL_TO(sqlExecutorIn, Global.sqe, createSqlExecutor())

Comparing this to the existing named value macros, it seems return types only work if they are given explicitly, but not when they are generics... (?) Is there a way to fix/improve this and avoid the cast (or equivalent "as ...") ? Was this improved in newer Grooyv versions (I searched the Groovy Jira, but found nothing in the very small number of tickets that has "macro" in their summary) ?

Thanks, cheers,
mg




Reply via email to