sourcepawn icon indicating copy to clipboard operation
sourcepawn copied to clipboard

"Lambda" functions

Open CrazyHackGUT opened this issue 7 years ago • 7 comments

Add support for lambda functions. Like this:

void DisplayDBTablesInServerConsole() {
    g_hDB.Query( [] (Database hDB, DBResultSet hResults, const char[] szError, any data) -> void {
        char szTable[64];
        while (hResults.FetchRow()) {
            hResults.FetchString(0, szTable, sizeof(szTable));
            PrintToServer("Found table in DB: %s", szTable);
        }
    }, "SHOW TABLES;", 0, DBPrio_Low);
}

CrazyHackGUT avatar Feb 16 '18 17:02 CrazyHackGUT

This use case was a motivating reason for the experimental v2 parser; it has almost zero chance of happening at the moment though. It’s out of scope for spcomp1 (making newfunc reentrant would be a nightmare) and v2 is not under active development.

It’s possible that, If we made newfunc reentrant, we could make your example work. But access to local scope would be impossible without v2 and a garbage collection safe API.

dvander avatar Feb 16 '18 18:02 dvander

Was support for lambda-type calls ever added to Sourcemod? j/w as I'd like to use them in my rpgmaker, if at all possible...

exSkye avatar Aug 22 '22 21:08 exSkye

With the new compiler, it's actually in the realm of feasible. But we're talking "can see it through the Hubble telescope".

dvander avatar Aug 22 '22 21:08 dvander

so, what needs to be done to get us a little-bit-closer?

exSkye avatar Aug 22 '22 23:08 exSkye

non-capturing lambdas could be done right now. capturing lambdas would need garbage collection, which is a pretty invasive set of changes.

dvander avatar Aug 22 '22 23:08 dvander

I'm really looking forward to this change, which means I won't have to write those extra callbacks😘

Nyano1337 avatar Aug 23 '22 09:08 Nyano1337

I have made an experimental test parse on adding function literals to SP, here's the grammar I got to:

Primary = identifier | ... | '(' Expr ')' | '{' Expr '}' | FuncLit .
FuncLit = SignatureSpec BlockStmt .
SignatureSpec = 'function' AbstractDecl ParamsList .
BlockStmt = '{' *Statement '}' .

kevyonan avatar Aug 23 '22 18:08 kevyonan