SHARE
TWEET

Untitled

a guest Nov 9th, 2012 76 Never
  1. struct Animal {
  2.     int delegate(int) fly;
  3.     void delegate() crawl;
  4. }
  5.  
  6. // Use structs
  7. struct Bird {
  8.     Animal animal;
  9.     alias animal this;
  10.    
  11.     Wing[] wings;
  12.    
  13.     this()
  14.     {
  15.         animal.fly = &flyImpl;
  16.         anumal.crawl = &crawlImpl;
  17.     }
  18.    
  19.     int flyImpl(int) { return use(wings); }
  20.     void crawlImpl() { }
  21. }
  22.  
  23. // Use closures
  24.  
  25. Animal makeBird()
  26. {
  27.     Animal bird;
  28.     Wing wings;
  29.    
  30.     bird.fly = (int) { return use(wings); }
  31.     bird.crawl = () {};
  32.     return bird;
  33. }
RAW Paste Data
Top