Makes sense, I think.
I’m swimming outside my depth, but it feels like there ought to be a way: because if I can do it, the compiler—which I trust is far more intelligent than I—ought to be able to ![]()
I would think that type-annotating a variable that’s going to be boxed, could get syntax-transformed into type-parameterized boxing. We shouldn’t need to know the type; just throw the expression into the curly braces. For example:
let a::MyType = 0
() -> (a += 1; a)
end
#= currently transforms into something like this: =#
let a = Core.Box(0)
() -> (a.contents = a.contents::MyType + 1; a.contents::MyType)
end
#= maybe better to transform into this: =#
let a = Core.Box{MyType}(0)
() -> (a.contents = a.contents::MyType + 1; a.contents::MyType)
end
this is basically what the Ref hack amounts to anyway. The type assertions become redundant, but maybe it’s easiest to leave them in, idk.
The compiler makes the decision to box based on analysis of the syntax, without type information, so it feels like a syntax transform (or something equivalent to one) ought to be the fix?
Note: the least invasive change might be like this:
mutable struct Box{C} contents::C end
Box(contents) = Box{Any}(contents)
then, any code that simply calls Box(x) doesn’t need to change immediately.