https://kotlinlang.org logo
Title
k

karelpeeters

08/21/2017, 11:43 AM
You can also check if it's null when calling the method.
d

davidase

08/21/2017, 11:51 AM
it's an information-dump page where i just list whatever information that is available for an object
$msg.get("PAYMENT_DATE", $transaction.paidDate)
$msg.get("PAYMENT_METHOD", $transaction.paidWith)
$msg.get("PAYMENT_AMOUNT", $transaction.totalAmountPreVat, $transaction.currency)
i don't really care if anything is null
i'm not really looking for alternate solutions, the approach i have currently works well for java
i was just wondering if there was a nicer way of writing the function
k

karelpeeters

08/21/2017, 12:00 PM
Then what did you mean with "varargs can't be null"? They certainly can, if you give them a nullable type like in @menegatti's example.
d

davidase

08/21/2017, 12:03 PM
then you'll have to pass an array of nulls, you can't pass just a singular null value
k

karelpeeters

08/21/2017, 12:08 PM
Can you give some example of how you want this to work?
get("key", "a", "b", "c")
get("key", "a", null)
get("key", null, "a")
get("key", null)
get("key")
get("key", *arrayOf("a", null))
get("key", *arrayOf<String>())
All of those would work with the vararg one.
d

davidase

08/21/2017, 12:09 PM
hmm,
get("key", null)
won't work for me
fun get(key: String, vararg args: Any?): String {
    return MessageFormat.format(get(key), *args)
}
Parameter specified as non-null is null: method util.MessageBundle.get, parameter args
😵 1
k

karelpeeters

08/21/2017, 12:13 PM
Huh that is strange, I wonder if it's intended.
d

davidase

08/21/2017, 12:14 PM
i made a new project and just ran your snippet, there everything works 🤔
k

karelpeeters

08/21/2017, 12:15 PM
Do both projects have different compiler versions?
d

davidase

08/21/2017, 12:15 PM
get
is being called from apache velocity in my original project
not sure why that would matter though
k

karelpeeters

08/21/2017, 12:16 PM
Did you actually run the broken one or is that just Idea complaining?
d

davidase

08/21/2017, 12:16 PM
i ran it
k

karelpeeters

08/21/2017, 12:18 PM
Compiler versions?
d

davidase

08/21/2017, 12:19 PM
org.apache.velocity.exception.MethodInvocationException: Invocation of method 'get' in  class util.MessageBundle threw exception java.lang.IllegalArgumentException: Parameter specified as non-null is null: method util.MessageBundle.get, parameter args at /velocity/views/data-dump.vm[line 5, column 41]
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method util.MessageBundle.get, parameter args
k

karelpeeters

08/21/2017, 12:20 PM
Right that's just Kotlin's build-in runtime null checking complaining.
d

davidase

08/21/2017, 12:27 PM
why would it complain when calling the function from velocity, but not from a
fun main
?
kotlin 1.1.2-2 
target jvm1.8 
javac 1.8.0_101
b

benleggiero

08/21/2017, 1:31 PM
Oh yeah in Java, there's no obvious way to distinguish
get("key", {1, 2, 3})
from
get("key", 1, 2, 3)
, so in Java,
get("key", null)
is like
get("key", *null)
in Kotlin. Assuming the calling code is Java
k

karelpeeters

08/21/2017, 1:34 PM
Ah that could be the difference, is the calling code Java?
d

davidase

08/21/2017, 1:51 PM
@benleggiero thanks, does that mean i'm stuck with this?
fun get(key: String, args: Array<Any?>?): String {
    if (args == null) return MessageFormat.format(get(key), args)
    return MessageFormat.format(get(key), *args)
}
@karelpeeters yes, sorry if i failed to make that clear
the function is being called from apache-velocity, a java-based template engine
k

karelpeeters

08/21/2017, 1:52 PM
You can overload it with a version that takes a single extra parameter and calls the other one.
1
d

davidase

08/21/2017, 1:55 PM
🤔