[Issue 3995] New: Can't access array/AA from function literal defined inside the array/AA's initializer

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 20 12:38:05 PDT 2010


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

           Summary: Can't access array/AA from function literal defined
                    inside the array/AA's initializer
           Product: D
           Version: 1.050
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: cbkbbejeap at mailinator.com
            Blocks: 340


--- Comment #0 from Nick Sabalausky <cbkbbejeap at mailinator.com> 2010-03-20 12:38:04 PDT ---
I find this idiom very useful, particularly for handling domain-specific
languages:

------------------------------
auto dgMap = [
    "foo": (int i) {
        return /+..stuff..+/;
    },
    "bar": (int i) {
        return /+..stuff..+/;
    },
    "bat": (int i) {
        return /+..stuff..+/;
    },
];
dgMap["bar"](4);
------------------------------

However, those functions cannot refer to each other or use recursion even
though doing so would be perfectly safe:

------------------------------
auto dgMap = [
    "foo": (int i) {
        if(i < 1)
            return 0;
        else
            // Recursion, would be perfectly safe if allowed.
            // ERROR: undefined identifier dgMap
            return dgMap["foo"](i-1);
    },

    "bar": (int i) {
        // Call different element: would be perfectly safe if allowed.
        // ERROR: undefined identifier dgMap
        return dgMap["foo"](7);;
    }
];
------------------------------

This is a bit of a pain, as it can't be solved without giving up either the
locality of the in-line delegate literals or the type inference of the array/AA
(which is *very* useful in this sort of situation).

I'm not sure if this counts as an enhancement or a declaration-order bug.

-- 
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