Closed lvcabral closed 5 months ago
RBI automatically boxes function parameters and return values. As it turns out, RBI also unboxes function parameters and return values to match a function's declared signature. Consider this example:
sub main()
boxed = createObject("roString")
boxed.setString("lorem ipsum")
print "type(boxed) = " type(boxed) " | boxed = " boxed
unboxed = unboxing(boxed)
print "type(unboxed) = " type(unboxed) " | unboxed = " unboxed
end sub
sub unboxing(s as string) as string
print "type(s) = " type(s) " | s = " s
return s
end sub
Inside of the unboxing
function, s
is always a primitive string
and never an instance of roString
. Similarly, the return value of unboxing
is also always a primitive string
and never an instance of roString
.
brs
currently doesn't handle this demotion properly, and throws type mismatch errors at runtime 😢
in
brs
it was implemented an auto-box when the code hasas object
in the parameters, the example below shows how it works, in theRightTest()
functionparam1
type isString
and inLeftTest()
functionparam1
type isroString
. Also theparam2
is auto-casted to boolean from Integer in theLeftTest()
The output of this code in a Roku: