leap icon indicating copy to clipboard operation
leap copied to clipboard

(request) Object destructuring

Open JaRoLoz opened this issue 2 years ago • 3 comments

Hi, it would be nice if this syntax could be implemented:

function someFunction({ prop1, prop2 })
    print(prop1, prop2)
end

JaRoLoz avatar Apr 10 '23 20:04 JaRoLoz

You can already do an object destruction but not in the parameters, example:

function someFunction(obj)
    prop1, prop2 = ...obj
    print(prop1, prop2)
end

However, we could also add the version you mentioned, have you seen similar syntax yet? (like a documentation of some languages or something) @MrFreex what do you think?

XenoS-ITA avatar Apr 20 '23 23:04 XenoS-ITA

I've been searching for any documentation on how to implement it, but haven't found anything. What i've came up with is just replacing the destructured object in the parameters with a dummy parameter name, such as __parameter, and then replacing the used propperties of the object inside the function with __parameter.property. Example:

# before preprocessing
function myFunction({ prop1, prop2 })
    print(prop1, prop2)
end
# after preprocessing
function myFunction(__parameter)
    print(__parameter.prop1, __parameter,prop2)
end

JaRoLoz avatar Apr 21 '23 14:04 JaRoLoz

that method could be quite a challenge since leap works on regex, it does not use AST or parser, so it is quite complicated to do it with regex.

XenoS-ITA avatar Apr 21 '23 17:04 XenoS-ITA