YoYoGames / GameMaker-Bugs

Public tracking for GameMaker bugs
20 stars 8 forks source link

Another way of making anonymous functions #4684

Open xijniN opened 6 months ago

xijniN commented 6 months ago

Is your feature request related to a problem?

A lot of times I need to make simple anonymous functions to function like array_filter, array_foreach and etc. But sometimes what I want to do is very simple, but I have to use 3 whole lines to do it, like in this example:

array_filter(_instances, function(_inst){
    return instance_exists(_inst);
});

Describe the solution you'd like

I think we could add another way of doing it(keeping the current way of doing it), like the arrow functions of javascript

array_filter(_instances, _inst => instance_exists(_inst));

I'm not asking to make the same thing, but something like that would be cool(it won't change anything of how the code works, but I think it's easier to use)

Describe alternatives you've considered

We could use something like the lambdas of python, but I think it's a little bit too different of how gamemaker code is usually written, but I think it's worth mentioning

array_filter(_instances, lambda _inst: instance_exists(_inst));

Additional context

I saw it when I was learning about GMEdit and I thought it was cool image

Alphish commented 6 months ago

The arrow function notation is what YYG considers along with having lambdas as closures (so that local variables from the outside are accessible within the function). See #3296 for more details.