http://coldfusion.com logo
#lucee
Title
# lucee
a

Adam Cameron

03/15/2022, 8:57 AM
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

dswitzer

03/15/2022, 3:29 PM
@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

bdw429s

03/15/2022, 7:15 PM
@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

Adam Cameron

03/15/2022, 7:15 PM
I think perhaps you need to re-read what I asked, Brad
You're not answering the question I'm asking
b

bdw429s

03/15/2022, 7:16 PM
re-reads...
ok, sorry
I see
a

Adam Cameron

03/15/2022, 7:16 PM
NP mate
b

bdw429s

03/15/2022, 7:16 PM
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

Adam Cameron

03/15/2022, 7:17 PM
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

Adam Cameron

03/15/2022, 7:17 PM
I can "see" why that might be an approach to have considered I guess
b

bdw429s

03/15/2022, 7:17 PM
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

Adam Cameron

03/15/2022, 7:19 PM
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

Adam Cameron

03/15/2022, 7:21 PM
(from RTFMing, not testing it...)
d

David Rogers

03/15/2022, 7:21 PM
looks like it tries very hard to find a match, bdw is right about auto/magic conversions
a

Adam Cameron

03/15/2022, 7:22 PM
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

David Rogers

03/15/2022, 7:26 PM
message has been deleted