Skip to content

Instantly share code, notes, and snippets.

@dherman
Created February 22, 2012 18:46
Show Gist options
  • Save dherman/1886598 to your computer and use it in GitHub Desktop.
Save dherman/1886598 to your computer and use it in GitHub Desktop.
simple macro-by-example macro
(define-syntax or
(syntax-rules ()
[(or e)
e]
[(or e1 es ...)
(let ([x e1])
(if x x (or es ...)))]))
@nikomatsakis
Copy link

I could then generate impls that define a tr method for all the types I might need.

@nikomatsakis
Copy link

Now to the gen_map_fn macro, you might do something like:

def_structural_map gen_map_fn(type_name, method_name, arg_name, arg_type) { v =>
    v.$method_name($arg_name)
}

obviously this is not real syntax. but the idea is something like "generate a pattern that uses alt or whatever to iterate over the members of an instance of the type type_name. For each member, generate the code shown (in this case, a call to a method)"

I see three similar patterns:

--- visit: walks and invokes the method but does not build a result
--- map: expects each method to generate a new instance of the given type

Deserialization is kind of a different kind of map where the input data is not an instance of the given type but rather a byte-stream...

...anyway, maybe this cannot be done or maybe macros are not the right tool. but it seems like it'd be useful to me.

...the advantage of this approach is that there is no need for any kind of AST API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment