Annoyance: 'Shadowing declaration is deprecated'+ mixins

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Wed Jul 25 04:11:30 PDT 2007


Don Clugston wrote:
> Jari-Matti Mäkelä wrote:

>> I hope that when macros come there will be a possibility to choose
>> between hygienic and unhygienic versions. I've started to think string
>> mixins are a big design mistake. Looking at those compile time unique id
>> hacks from Lisp coder's POV it's amazing to see how badly you can
>> implement something so obvious. I wrote about macros a while ago and
>> noticed that by increasing the power of template mixins and alias
>> parameters it would be possible to achieve the same things and more
>> without sacrificing e.g. syntactical transparency.
> 
> My post was actually to indicate that this lack of hygiene is a genuine
> nuisance (not merely aesthetically undesirable). I wonder when an
> unhygienic macro would actually be useful.

Speaking of aesthetics makes it sound like it's only some academic nonsense
not applicable in "real world". I've experienced the same problems even
with very short metaprograms and created ugly looking ad-hoc workarounds.
Most other languages solve the problem using similar techniques so I just
wanted to tell there's no need to reimplement the wheel this time. This is
a bad example, but hopefully highlights some of the problems with current
constructs:

import tango.io.Stdout;

char[] a, i;

char[] foo(char[] a) {
  return "for (int i=0; i<"~a~".length;i++)
  Stdout("~a~"[i]).newline;";
}

mixin GenNewUniqPrefix!(); // defined elsewhere
char[] ad_hoc_foo(char[] a) {
  char[] safe_prefix = getLastUniqPrefix(); // defined elsewhere

  char[] safe_a = safe_prefix ~ "a";
  char[] safe_i = safe_prefix ~ "i";

  return "for (int "~safe_i~"=0; i<"~safe_a ~".length;"~safe_i~"++)
  Stdout("~safe_a~"["~safe_i~"]).newline;";
}

template hygienic_foo(alias a) {
  void hygienic_foo() {
    for(int i=0; i<a.length; i++)
      Stdout(a[i]).newline;
  }
}

/* not implemented yet
macro macro_foo(alias a) {
  for(int i=0; i<a.length; i++)
    Stdout(a[i]).newline;
}
*/

void main() {
  int[3] bar = [1,2,3];
  class i {}
  mixin(foo("bar")); // collision
  hygienic_foo!(bar)();
  macro_foo(bar); // not implemented yet
}



More information about the Digitalmars-d mailing list