Am wondering if someone can cast some light on Luc...
# lucee
a
Am wondering if someone can cast some light on Lucee's inner workings when it comes to creating Java objects. Here's a gist: https://trycf.com/gist/8b67cae6202d341ed8ade82c913a5250/lucee5?theme=monokai Relevant bit:
Copy code
try {
    radix = 36
    result = CreateObject("java", "java.math.BigInteger").init("42", radix)
    writeDump([result, radix.getClass().getName()]) // 146, java.lang.Double
} catch (any e) {
    writeDump([e.message, e.detail])
}
What I'm wondering is... given
radix
is a Double, and BigInteger doesn't implement a constructor that takes a Double for the radix (see https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigInteger.html), how this code runs, rather than giving something along the lines of "no suitable constructor found for BigInteger(String,Double)"
d
@Adam Cameron I thought it might be casting the double to an int, seeing that the value could be safely converted to an int, but changing the value to a decimal still does not throw an error.
b
@Adam Cameron You're dumping out the class name of the
radix
variable, not the
result
variable.
This works fine from the CommandBox REPL
Copy code
CFSCRIPT-REPL: CreateObject("java", "java.math.BigInteger").init(2,36).getClass().getName()
java.math.BigInteger
Copy code
radix = 36
☝️ But this is going to be a Double
a
I think perhaps you need to re-read what I asked, Brad
You're not answering the question I'm asking
b
re-reads...
ok, sorry
I see
a
NP mate
b
What happens is Lucee, upon not finding a perfect match for the constructor args, attempts to see if it can cast your args to suitable values
a
I'm just wondering why using the "wrong" type works
Yeah. Not sure it should be doing that. But if that's what it's doing: so be it
a
I can "see" why that might be an approach to have considered I guess
b
Since it figured it could cast your double to a string
The behavior is handy in a lot of cases I'll admin where I can just leave off my javaCast() and Lucee will figure it out.
But I agree, it adds a certain amount of ambiguity to the process
1
a
I guess the radix param is an int not an Integer so perhaps not so straight fwd to create a Java primitive in CFML
I did not check if Java will automatically unbox an Integer though
Looks like it does.
a
(from RTFMing, not testing it...)
d
looks like it tries very hard to find a match, bdw is right about auto/magic conversions
a
Still: I think Java is well and clearly documented, so just leaving out the magic would have been fine I think.
Anyway, cheers for digging through the code!
d
message has been deleted