using eval in a macro is generally going to give you the wrong answer. In 0.4, there are staged functions: https://github.com/JuliaLang/julia/issues/7311 which may suit your use case.
But really, in this case, it doesn't seem like you need anything more than a function my_set_rounding(::Float64) = set_rounding(Float64, RoundDown) my_set_rounding(::BigFloat) = set_rounding(BigFloat, RoundNearest) On Thu, Oct 9, 2014 at 8:38 PM, David P. Sanders <dpsand...@gmail.com> wrote: > Hi, > > For the ValidatedNumerics package, I am trying to define macros for > rounding that change the corresponding rounding mode depending on the type > of their argument, Float64 or BigFloat. > (Currently only BigFloats are used, but Float64s are much faster.) > > The following seems to work, but it also seems to be tricky and fragile. > Is there a better way? > > ``` > function set_rounding_mode(expr, rounding_mode) > value = @eval($expr) > T = typeof(value) > :(set_rounding($T, $rounding_mode)) > end > > macro round_down(expr) > round_expr = set_rounding_mode(expr, RoundDown) > expr = :($round_expr; $expr) > end > > @round_down(0.1) > ``` > > However, if I now want to reset the rounding mode after performing the > operation, as follows > > ``` > macro round_down(expr) > round_expr1 = set_rounding_mode(expr, RoundDown) > round_expr2 = set_rounding_mode(expr, RoundNearest) > expr = :($round_expr1; $expr; $round_expr2) > end > > @round_down(0.1) > ``` > > it returns 0, since the second call to set_rounding_mode returns the > rounding mode, which is 0. > > Surely I am missing some subtle issues about macro hygiene, escaping, and > such like. > Please help! > > Thanks, > David. > > >