Macro expansion

I tried to make macro that will return type depending on type I pass to it as argument
For example:

macro_rules! foo { ($t:ty) => (Option::<$t>) }

Everithing is OK until I try to do recursive macro invocation:


This should transform to Option::<Option::<isize>>. Instaed I get compilation error.

src/ 77:12 error: no rules expected the token `!`
src/   foo!(foo!(i32));

Is there any method to force macro expansion in place, before it will be passed into another macro?
Or maybe it not possible at all?
Or may be there some workaround you know?

Shouldn't it be Option<Option<isize>>? I'm pretty sure the :: is not supposed to be there in this context.

You can't. As per The Rust Reference has moved,

Currently, macros can expand to expressions, statements, items, or patterns.

You can have a macro that expands out to a statement, like this:


macro_rules! foo (
    ($x:expr) => (Some($x)); 
    ($x:ident) => (Some($x)); 

fn main() {
  let x = foo!(100);
  let y = foo!(x);
  println!("{:?}", y);

...but not a macro that expands to a type, like this:

struct Foo {
  bar: foo!(isize)

The error you're getting is because you're trying to use a macro in a space where it's no valid, not that it's a recursive call.