-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object deconstruct #13
Comments
I'm not sure about this because:
I'm not sure if its usefulness is greater than the complexity that adds to read the script. |
I'm also concerned about the usefulness relative to the readability. Something I see common in other languages is the ability to assign multiple variables with one statement. It'd be convenient to have something like this supported:
It makes the decomposition of objects less annoying, but has a syntax that more programmers would already be familiar with. For what it's worth, clojure has a similar syntax, they call it "destructuring" - https://clojure.org/guides/destructuring |
https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct#deconstructing-a-tuple
https://users.rust-lang.org/t/tuples-destructuring-assignment/11028
https://docs.scala-lang.org/tour/tuples.html They all have it for tuples but as we don't have tuples (because I still think we don't need them) I thought we can make it for objects |
I'm going to show some examples mixed with arrays/tuples but those are the same for DW. C++ also have it in this shapeauto [x,y,z] = vectorTouple; Which is the same as JS/TS
let [x,y,z] = [1,2,3] And JS/TS has the object destructuring in its simpler form let {x,y,z} = vector And with renames let {x: horizontal,y: vertical,z: depth} = vector
// you can use horizontal now Do not forget the rest operation: let {x, ...resto} = vector3
// resto = {y,z} Also for arrays: let [first, ...resto] = [1,2,3,4]
// resto = [2,3,4] Which in dataweave is the Go, python and the WASM and the LLVM have multiple return valuesfunc x() {
return 1,2,3,4
}
a,b,c,d = x() |
Object decomposition
It is very common to return objects and then trying to extract its parts.
For example the function
splitAt
ofdw::core::Arrays
returns aPair
that is basically an object withl
andr
propertiesSo currently what we need to do is
I think this verbose with no reason and that many modern language already solve this.
Proposal
Binding top level fields
We just allow to bind any part of the result.
Also not the entire object needs to be bind. For example if we want just to bind the
r
we can justBinding inner fields
var {error: {kind: kind}} = try(splitAt([1,2,3], 2))
This will bind the
error.kind
fieldThe text was updated successfully, but these errors were encountered: