Which one would you use??
I wrote this post with my own views and wider explanations, but I wanted to compile here the main difference in syntax.
do
%dw 2.0
output application/json
var age = 21
---
{
person: do {
var user = "Robin"
var age = 5
---
{
name: user,
age: age
}
}
}
using
%dw 2.0
output application/json
var age = 21
---
{
person: using (user = "Robin", age = "5") {
name: user,
age: age
}
}
Besides the fact that using
is deprecated in DataWeave 2.0, I prefer to use do
way more because of the syntax. For me it is more intuitive to know where to define local functions or variables. I think you can't define named functions with using
, but you can create lambda expressions (which is a whole can of worms on its own!).
What do you think though?
Top comments (1)
I think comparing
do
andusing
may not be the best approach to explain the difference sinceusing
is deprecated, and its use shouldn't be advertised at all. It was kept for backward compatibility, as mentioned in the official doc and thus no new code should be written using it.To avoid confusing learners / discorage the use of a deprecated and (now) poorly documented operator, reworking the blog post to explain how to move from
using
todo
, and providing a more clear warning about not usingusing
would be a better approach.It is sometimes hard for junior developers to understand why something should not be used when they have not yet experienced deprecation (I did the mistake too when learning programming!).