Hello,
I have an Idea I would like to implement for a module I am working on. I will try to explain, but I am not sure if such implementation would make much sense, probably there are better solutions.
I have a macro, that looks somewhat like the following:
abstract type SomeThing end
macro some_macro(name, list, func)
str = :(
struct $name <: SomeThing
list::Tuple
func::Function
function $name(list::Tuple)
return new(list, $func)
end
end
)
return str
end
The module user should then be able to add their own struct, containing a user defined method which is then called in another part of the code. Using the macro shall ensure, that the structure that is needed is contained.
It is then created with
@some_macro Name (:a, :b) function (vec)
# Do something with the parameters in the list and vec -> result
return # result
end
However, the input vector can be of length 3 or 6. Right now, the user has to implement a routine for each case, but I would like to have it simplified that only one is necessary. For this, I would like to have an additional dispatch included in the macro. It should do something like this:
if length(vec) == 3
# transform to length 6 with zeros
new_vec[1] = vec[1]
#...
new_vec[3] = 0
#...
new_vec[6] = 0
else
# keep lengt,
end
However, the routine where the struct is used calls the function defined in the second codeblock like this:
Struc1.Struc2.List[i].func(vec)
I hope it is understandable what I want to achive. If so, how would I go for this?
I think I pretty much want to append the if/else in front of the users function. But How would I dismantle the function to do so?