Okay, following up briefly on the hygiene / path discussion at the meeting. 

Niko asks about a macro that accepts a path element (or, more specifically, a 
prefix). Specifically, what about a macro that accepts the name of a module and 
then uses several functions defined in that module?

So, for instance, suppose you want a macro that accepts the name of a module 
that includes map functions, and creates a new map where z is bound to 4.

This may or may not work right now:

macro_rules! map_z_to_4 (
  ($MAPMODULE:ident) => 
  (let new_map = $MAPMODULE::new();
   $MAPMODULE::bind(~"z",4))
)

First off, I believe that currently, this will work if $MAPMODULE is a single 
alnum sequence, but not if it's something like "std::hashmap"… because that's 
not an identifier.  In fact, that kind of makes my point right there :).

In a solution where paths are treated as the basic hygiene element, I would 
expect to have to write something like this, depending on what the syntax for a 
capturing macro is:

macro_rules! map_z_to_4 (
  ($MAPMODULE:ident) => 
  (let new_map = capture!($MAPMODULE::new());
   capture!($MAPMODULE::bind)(~"z",4))
)

… or you can imagine some other syntax.

The word "capture" here may not be the right choice; the idea is that when we 
want to construct a reference artificially (in this case, by gluing together a 
module name and a variable name), that this would be explicitly called out. 


I should also add that in this situation, it seems like it would be more 
natural just to pass around an object or trait with methods, rather than using 
a macro at all.

Does that make sense and/or seem desirable?

John

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to