Yuescript icon indicating copy to clipboard operation
Yuescript copied to clipboard

[Feature Request] Const Parameters

Open TurtleP opened this issue 2 years ago • 6 comments

Since yuescript does support constvariables (for lua 5.4), I've been playing around with it for a little bit. However, one thing I noticed is that a const parameter cannot be created. I'm not sure if this a thing in lua 5.4 (I've done some Google searching, can't find it), but it would be ideal to allow const in parameters. For example:

test = (const a, b) ->
    a = 3
    return a + b

a = 1
const b = 2

print test a, b

I build this:

 ❯ yue.exe --target=5.1 test/main.yue
Built test/main.yue

It results in:

local test = (const(a, b))(function()
    local a = 3
    return a + b
end)
local a = 1
local b = 2
return print(test(a, b))

Now, even if I built it for lua 5.4, it results in the same compilation, which obviously const isn't supposed to be a valid function here, since I never defined it. I want that to mark the parameter as const so I cannot modify a, or whatever the parameter might be, in the function. I think it should be doable, honestly, because even compiling with const variables for lua 5.1 works perfectly fine, but if you were to modify the variable, yue refuses to build the source file as it should.

Even if const parameters don't exist in lua 5.4, it would be easy to add a check that the variable is marked as such in the parameter and refuse to build the file if you try to modify it, much like with a variable.

TurtleP avatar Dec 29 '22 18:12 TurtleP

The 'const' and 'close' labels are not treated as keywords in Yue for the moment. So you are getting this 'weird' compiling result. And there might be some more rules to discuss. Should the 'const' label be applied for individual param instead of all the params? Or should we just make the all the function params be const by default to ensure a better coding practice?

pigpigyyy avatar Dec 30 '22 06:12 pigpigyyy

I think it would make more sense to align them like how various other languages are, that is, to treat each one as const separately (meaning you have to mark each one you want in the parameter list).

add = (const a, const b) ->
  return a + b

If everything were marked const by default, I think it might cause more issues than not.

One other situation to consider might be varargs, (...). Maybe marking that const would make all of the items contained inside const.

TurtleP avatar Dec 30 '22 16:12 TurtleP

I thinks 'const' parameters don't exist in lua 5.4. And I don't see it in most langs... Well it in bloated C++. I don't think it usefully enough to justify implement in any dynamic languages like lua/yuescript because it feature should more fit to be in a linter, not in language it self. Implement it in YueScript without a thoughtful consider then I just make language become more bloated and prone to more errors. Lua 5.4 won't have it then Yue should not have. Thanks and regards!

GokuHiki avatar Jan 07 '23 06:01 GokuHiki

I noticed #143 was recently added to yuescript. Do you think this is also feasible to do?

TurtleP avatar Aug 16 '23 14:08 TurtleP

Implementing this feature won't be a problem. But I'm thinking of making function params const by default that can be more helpful according to my coding experiences. And I'm expecting more users' opinion before adding this feature. For the moment you can use a little trick to mark function parameters constant:

test = (a, b) ->
  const a, b = a, b
  a = 3 -- report error by compiler
  return a + b

or make use of a macro:

macro const = (...) ->
  vars = table.concat {...}, ","
  "const #{vars} = #{vars}"

test = (a, b) ->
  $const a, b
  a = 3 -- report error by compiler
  return a + b

pigpigyyy avatar Aug 17 '23 07:08 pigpigyyy

Gotcha, makes sense. Though my concern about defaulting parameters to const would be, for example, a function that can optionally take a table and then have it used as an out parameter.

my_table = {}
test = (path, some_table) ->
  -- fill some_table here

Granted not a great code example, but in the instance you would reuse this function for other tables and not just for my_table.

TurtleP avatar Aug 17 '23 16:08 TurtleP