Hi Is it possible to use macros inside o functions in Julia if they are 
expanded at compile time as the manual says? I've been playing with a toy 
type:

type MeltingTemperature
  value::Int
end




myvar = MeltingTemperature(56)


and say I want to make a function that write it out as an XML string: 
"<MeltingTemperature>56</MeltingTemperature>

Now I want to make a function that constructs that string by 1). Adding the 
open tag 2). Checking the temperature is not nothing and if it is not, 
including it in the string. 3). add the closing tag.

I figured step 2 would be a good candidate for a macro for a larger type as 
checking each value is not nothing would be tedious. So:

macro INN(string, value)
  value != nothing ? "$string$value" : "$string"
end


function writePhyXML(x::MeltingTemperature)
  outstring = "<MeltTemp>"
  outstring = @INN outstring x.value;
  outstring = "$(outstring)</MeltTemp>"
  outstring
end


I figured the macro would be expanded and the function definition would 
become:

function writePhyXML(x::MeltingTemperature)
  outstring = "<MeltTemp>"
  outstring = x.value != nothing ? "$outstring$x.value" : "$outstring"
  outstring = "$(outstring)</MeltTemp>"
  outstring
end


But results in "outstringx.value</MeltTemp>"

Is it possible to do this with macro's? I had presumed it is.

(I know it's possible by just using a sub function, but I wanted to see how 
to try and use macros to generate code for function definitions if 
possible.)

Thanks,
Ben.

Reply via email to