Yep, thanks for pointing that out :) But I'm not sure that helps with what
I'm saying, since inside a function I won't be able to access the variables
x and y directly without using dot notation:
function distance (point::Point)
# now you have to use point.x and point.y which is quite obfuscated eg:
sqrt (point.x*point.x + point.y*point.y)
end
when really I would like to be able to type (
https://groups.google.com/forum/#!topic/julia-users/UvBff9QVKaA ):
function distance (point::point)
using point
sqrt (x*x + y*y)
end
or
function distance (point::Point)
@fetch point
sqrt (x*x + y*y)
end
For the above cases this isn't a big deal, but I have algorithms that
require 30 to 100 so state variables like this the entire algorithm starts
becoming unreadable since all the eye can see is point.this and point.that
not just this and that.
On Sunday, June 8, 2014 12:30:33 AM UTC+8, Jameson wrote:
>
> A macro can't do this because a macro can't see types. But I don't need a
> macro to get an even more efficient version of your attempted optimization.
> I just add the immutable keyword:
>
> *immutable* Point
> x::Float64
> y::Float64
> end
>
>
>
>
> On Sat, Jun 7, 2014 at 3:35 AM, Andrew Simper <[email protected]
> <javascript:>> wrote:
>
>> A lot of the time it is good to copy a structure to local variables and
>> then process on those for efficiency before storing the local values back
>> to a structure. To help out with this I'm trying to write a macro, so this
>> is what I would like the end result to be:
>>
>> Point
>> x::Float64
>> y::Float64
>> end
>>
>> function process (p::Point)
>> local x = p.x;
>> local y = p.y;
>> # do some processing on x and y
>> p.x = x
>> p.y = y
>> end
>>
>> and I would like write a macro that does this so the end code would like
>> like:
>>
>> function process (p::Point)
>> @fetch p
>> # do some processing on x and y
>> @store p
>> end
>>
>> So far I've got this working at the REPL using:
>>
>> point = Point (1, 2)
>> map (eval, ([:($name = point.$name) for name in names(point)]))
>> println("x=$(x) y=$(y)")
>>
>> which prints out: x=1.0 y=2.0
>>
>> Can someone please help out turning this into a macro?
>>
>>
>