0

let's say i have these two macros which are identical except for the macro name:

macro h1 {
  case {$name ($x (,) ...)} => {
    letstx $nameVal = [makeValue(unwrapSyntax(#{$name}), null)]
    return #{React.createElement($nameVal, $x (,) ...)}
  }
}

macro h2 {
  case {$name ($x (,) ...)} => {
    letstx $nameVal = [makeValue(unwrapSyntax(#{$name}), null)]
    return #{React.createElement($nameVal, $x (,) ...)}
  }
}

what are my options for code reuse here? can i have a macro generate a macro?

or could i minimally place the body portion (beginning with letstx...) in it's own 'internal' macro?:

tony_k
  • 1,983
  • 2
  • 20
  • 27

1 Answers1

1

How about something like:

macro make_header {
  case {_ $name ($x (,) ...)} => {
    letstx $nameVal = [makeValue(unwrapSyntax(#{$name}), null)]
    return #{React.createElement($nameVal, $x (,) ...)}
  }
}

macro h1 {
  rule { ($x (,) ...) } => {
    make_header h1 ($x (,) ...) 
  }
}
macro h2 {
  rule { ($x (,) ...) } => {
    make_header h2 ($x (,) ...) 
  }
}

h1 (1, 2, 3)
h2 (1, 2, 3)
timdisney
  • 5,287
  • 9
  • 35
  • 31
  • that works tim, not as dry as i would like, but certainly acceptable. can you think of any wizardry to clone out a macro? cause certainly "cloneMyMacro(h1); cloneMyMacro(h2);" would be even better... – tony_k Feb 28 '15 at 22:39