[Issue 7135] New: [tdpl] Multiple delegate-related issues (literal syntax, @system deduction)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Dec 18 21:08:53 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=7135

           Summary: [tdpl] Multiple delegate-related issues (literal
                    syntax, @system deduction)
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrei at metalanguage.com


--- Comment #0 from Andrei Alexandrescu <andrei at metalanguage.com> 2011-12-18 21:08:50 PST ---
This is a copy of a failing TDPL example. First, the compiler doesn't accept
the delegate literal syntax unless it includes the keyword "delegate". Fixing
that somehow makes the compiler decide the literal is @system and refuses
binding it.

module test;// Context begin
#line 1369 "0900-operators.tex"
import std.variant;

alias Variant delegate(Dynamic self, Variant[] args...) DynMethod;
// Context end
// Context begin
#line 1383 "0900-operators.tex"
class Dynamic {
   private DynMethod[string] methods;
   void addMethod(string name, DynMethod m) {
      methods[name] = m;
   }
   void removeMethod(string name) {
      methods.remove(name);
   }
   // Dispatch dynamically on method
   Variant call(string methodName, Variant[] args...) {
      return methods[methodName](this, args);
   }
   // Provide syntactic sugar with opDispatch
   Variant opDispatch(string m, Args)(Args args...) {
      Variant[] packedArgs = new Variant[args.length];
      foreach (i, arg; args) {
         packedArgs[i] = Variant(arg);
      }
      return call(m, args);
   }
}
// Context end
#line 1409 "0900-operators.tex"
unittest {
  import std.stdio;
  auto obj = new Dynamic;
  obj.addMethod("sayHello",
      delegate Variant(Dynamic, Variant[]) {
        writeln("Hello, world!");
        return Variant();
     });
  obj.sayHello();  // Prints "Hello, world!"
}

void main(){}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list