Useful stuff, Greg. I would like to see the way you implemented handing copy constructors, unary operators, etc. Would you mind collecting them in a gist or posting a link to the file[s]?
On Wednesday, December 16, 2015 at 5:22:24 AM UTC-5, Greg Plowman wrote: > > I have exactly the same requirement. > Additionally, I often have more than 2 fields and also change the fields > of my custom types. > > So I use a slightly more general version of the above: > > > function CompositeBinaryOp(T::Symbol, op::Symbol) > expressions = [ :($op(x1.$field, x2.$field)) for field in > fieldnames(eval(T)) ] > body = Expr(:call, T, expressions...) > quote > function $op(x1::$T, x2::$T) > return $body > end > end > end > > type M > a > b > end > > import Base: +, -, *, /, ^ > > for T in [:M] > for op in [:+, :-, :*, :/, :^] > #eval(CompositeBinaryOp(T, op)) > > code = CompositeBinaryOp(T, op) > println(code, "\n") > eval(code) > end > end > > > The advantage here for me is that I can change the fields (number, type > order etc.) and operators don't need manual updating. > > I have similar functions for copy constructors, unary operators etc. >
