I am having this problem which I can solve best if I could find out the inferred type of certain variables in a function. Basically, I am looking for a generalized
Core.Compiler.return_type for arbitrary variables, not just the returned ones. Related thread: Ridiculous idea: types from the future. Unfortunately, in my case it is not a simple map so I cannot use the solution from there. The array buffer would work for me, but is less than ideal. I thought of manipulating the function with a macro (that’s an option) to return the variables I am interested in, then use
Core.Compiler.return_type but then I have to worry about multiple return points in the original function, which is not robust I feel due to
throws, macros, etc which may be in the function body. Any suggestions? A toy MWE is simple:
a = x * 2
y = a / 3 # what is the inferred type of y?
@code_warntype prints the info in the repl and
typeof(y) gives you the type of y at runtime.
What I am after is something like the output of
@code_warntype programmatically at compile time.
Perhaps looking at the implementation of code_wantype can hint at what functionality is used there?
I would either use a function barrier, or
typeof as @baggepinnen suggested above. If the type can be inferred, this is costless, if not, then you have a fallback.
Oh well, perhaps moving it to internals will get more views from core devs before I give up on this. I will use the function barrier with the buffer if nothing else works.
What are you trying to do with the information?
I want to make a parameterized struct with a type parameter as the type of
y is defined. But it is not just
y, it can also be
a and other variables if I want, some of these may be arrays. The use case is a bit involved so I am not sure more information will be too helpful here. If what I am asking is not possible, I will assume the parameter is
Any at first, then narrow it down later.
I believe that an example that demonstrates more context would be very useful. The specific solution you are after may not be the idiomatic one, there may be another way.