I am trying to define this function (with “Int” as a string argument) through a macro
Here is my best try (which fails).
I realize this MWE is not meaningful. In my use case, the body of the function will be different (i.e. a different calculation) based on the type of
x (say the output is x^2 for Int32 and x^3 for UInt8).
end #end quote
Does typeof() not help here?
typeof(x)==Int32 && output=x^2
typeof(x)==UInt8 && output=x^3
Not for my use case. Certainly I can do
But for my actual use case, the macro pulls parameters from a dataframe generating a polynomial used in a hot inner loop. And these parameters will depend on the type of
x. Thus the body of the function will be different for each type of
I guess there might be other ways to approach my problem setting. But I would still like to know how (or if), I can write a macro which defines the function mentioned in my first snippet.
Ok. This does work.
I guess the question may come back to:
I can solve a problem by using “if then else”, a dictionary, multiple dispatch, …
Let us assume that I have 80 different types for which I have a different function body.
I wanted the functions to depend on the type of the input.
Certainly I can use 80 if conditions to achieve this. Would it be preferable over multiple dispatch?
You have to call your macro with a type, not a string, like this
julia> @myMacro Int
my_function_name (generic function with 1 method)
If you really want the macro to accept strings instead, you have to write your macro more like this:
end #end quote
I had a mixup with Base.parse and Meta.parse earlier today. I now realize that these are different functions.