Yuescript icon indicating copy to clipboard operation
Yuescript copied to clipboard

Add features to skip/optional destruction variables in the destructor!

Open thesolitudeofnoblesoul opened this issue 3 years ago • 3 comments

It would be great if YueScript implemented this skip unneeded member in list-like collection destruction. EX:

txt = "https://yuescript.org/try/"
parts = [p for p in txt\gmatch "[%w]+"]
{, domain, , sub} = parts

And in the dict-like collection we have optional key existed check condition before assign value. EX:

dict = {
  {}
  {1,2,3}
  a: {
    b: {
      c: 1
    }
  }
  x: y: z: 1
}
{
  first
  ?{one, two, three}
  a?: {
    b?: {
      :c
    }
  }
  x?: y?: :z
} = dict

To Lua:

local dict = {
  { },
  {
    1,
    2,
    3
  },
  a = {
    b = {
      c = 1
    }
  },
  x = {
    y = {
      z = 1
    }
  }
}
local first = dict[1]
local one, two, three
do
  local _obj_0 =  = dict[2]
  if _obj_0 ~= nil then
    one, two, three = _obj_0[1], _obj_0[2], _obj_0[3]
  end
end
local c
do
  local _obj_0 = dict.a
  if _obj_0 ~= nil then
    do
      local _obj_1 = _obj_0.b
      if _obj_1 ~= nil then
        c = _obj_1.c
      end
    end
  end
end
local z
do
  local _obj_0 = dict.x
  if _obj_0 ~= nil then
    do
      local _obj_1 = _obj_0.y
      if _obj_1 ~= nil then
        z = _obj_1.z
      end
    end
  end
end

I think it would be a nice addition feature to language. What your oppinion?

Thanks and regards!

thesolitudeofnoblesoul avatar Apr 19 '22 03:04 thesolitudeofnoblesoul

I think what you are proposing could be a set of pattern matching syntax. For example something like what the Fennel language does https://fennel-lang.org/reference#match-pattern-matching. And we make it a Yuescript version for dealing with table structure matching and destruction and other usages included.

And as for skipping items in a list destruction, I currently use an unused variable _ as placeholders. Maybe we can make _ a real placeholder to prevent generating unused codes.

txt = "https://yuescript.org/try/"
parts = [p for p in txt\gmatch "[%w]+"]
{_, domain, _, sub} = parts

pigpigyyy avatar Apr 19 '22 06:04 pigpigyyy

Many programing languages have include pattern matching feature. Pattern matching would be a great help for dealing with complex struct data/code when embed script in other languages/app.

Make _ a real placeholder to generate more better code and maybe improve performance would be welcome.

Thanks and regards!

thesolitudeofnoblesoul avatar Apr 19 '22 09:04 thesolitudeofnoblesoul

Added a pattern matching syntax to do the optional destructuring with the latest commits.

dict = {
  {}
  {1, 2, 3}
  a: {
    b: {
      c: 4
    }
  }
}
      
switch dict
  when {
      first
      {one, two, three}
      a: b: :c
      x: y: {:z = 5} -- if this structure fails to match, provide a default value and change match state to success
    }
    print first, one, two, three, c, z

--[[
output: 
table: 0x52eb38
1
2
3
4
5
]]

pigpigyyy avatar Jul 12 '22 16:07 pigpigyyy