On Wed, May 25, 2016 at 8:42 AM, Ford Ox <[email protected]> wrote: > From julia box > > macro m(a, b) > @show isa(a,Int) isa(b,Symbol) > end > > m(1,last) >>> >>> isa(a,Int) => true >>> >>> isa(b,Symbol) => true
This is NOT what you want. It shows you the type of the AST, which for basic constant happens to be what the constant value is If you have `a = 1; @m(a, last)` you will wee `a` is a symbol. In general, macro expansion by definition happens at syntax level and the macro can't possibly have more information than then parser itself. Lowering to a macro effectively moves the lowering to julia code and for this case I don't see what can be gained and you'll add a **lot** of overhead by calling back and forth between the parser and julia code. > > > > Dne středa 25. května 2016 14:26:30 UTC+2 Yichao Yu napsal(a): >> >> >> On May 25, 2016 8:19 AM, "Ford Ox" <[email protected]> wrote: >> > >> > Lets change compiler so it replace >> > array[i] >> > >> > with >> > @getindex(array, i) >> > >> > instead of >> > getindex(array, i) >> > >> > What we gain: >> > >> > array[end] >> > >> > :end can be converted to length(array) in julia itself which is nice! >> > >> > collections (dict, set, queue...) >> > >> > dequeue = ... # its a linked list >> > dequeue[first] >> > dequeue[last] # IMO :last causes less confusion than :end for new >> > programmers, but it doesn't really matter >> > dequeue[3] >> > >> > >> > macro getindex(q:Queue, key) >> > key == :first && return :(peekfirst(q)) # complexity O(1) >> > key == :last && return :(peeklast(q)) # complexity O(1) >> > isa(key, Int) && return :(peek(q, key)) # complexity O(n) >> > end >> >> No, the type info is not available at syntax level. >> >> > >> > >> > let's say that we would also have special syntax for >> > >> > popindex() >> > array[index]-- #completely made up >> > >> > and insertindex() >> > array[index]++ >> > >> > Then we could do the similar thing as above >> > macro popindex(q:Queue, key) >> > key == :first && return :(shift!(q)) >> > key == :last && return :(polll!(q)) >> > isa(key, Int) && return :(deleteat!(q, key)) >> > end >> > macro insertindex ..... >> > >> > >> > This looks pretty nice, don't you think? >> > >> > We could even some crazy stuff like this: >> > type Foo >> > a; b; c; d >> > end >> > >> > macro setindex(f::Foo, key, value) >> > return :(setfield(f, key, value)) >> > end >> > >> > f = Foo(1, 2, 3, 4) >> > f[c] = 10 >> > Seems like we don't need the dot accessing aka foo.var huh? >> >
