local var <const>

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

local var <const>

Fangrui Song
> 3.3.7 – Local Declarations
> ...
> There are two possible attributes: const, which declares a constant variable, that is, a variable that cannot be assigned to after its initialization; ...

local var <const> = whatever

Constant variables help reasoning about the code, but I think the
current syntax is a bit verbose, and may limit its adoption.
A concise syntax that declares the const property may ease its adoption. For
example: `const var = whatever`


Some languages that make declarations of constant variables simple:

OCaml: let foo =     vs    let mut foo =
Scala: val foo =     vs    var foo     =
Rust:  let x   =     vs    let mut x   =

Reply | Threaded
Open this post in threaded view
|

Re: local var <const>

Pierre Chapuis
> Constant variables help reasoning about the code, but I think the
> current syntax is a bit verbose, and may limit its adoption.
> A concise syntax that declares the const property may ease its adoption. For
> example: `const var = whatever`

For what it's worth, I agree with this.

--
Pierre Chapuis

Reply | Threaded
Open this post in threaded view
|

Re: local var <const>

Egor Skriptunoff-2
On Wed, Oct 9, 2019 at 8:25 PM Pierre Chapuis wrote:
> Constant variables help reasoning about the code, but I think the
> current syntax is a bit verbose, and may limit its adoption.
> A concise syntax that declares the const property may ease its adoption. For
> example: `const var = whatever`

For what it's worth, I agree with this.



+1
Reply | Threaded
Open this post in threaded view
|

Re: local var <const>

Sergey Kovalev
I think there is no need in const at all.

function ups(nane <const>)
  print("name=",name) -- mistype in argument name
end

ups "name"

I see no real application for const in dynamic language like lua

local t <const> = { x=1 }
t.x=2 -- wtf?

If you need const. It could be emulated in runtime using metatables:
function const(c)
    return setmetatable({},{__index=c,
        __newindex=function(t,n,v) error "const is read only" end
    })
end
local my = const { x=1, y=2, e=2.71, pi=3.14, person=const { name="a", age=1 } }
print(my.pi)

ср, 9 окт. 2019 г. в 21:15, Egor Skriptunoff <[hidden email]>:

>
> On Wed, Oct 9, 2019 at 8:25 PM Pierre Chapuis wrote:
>>
>> > Constant variables help reasoning about the code, but I think the
>> > current syntax is a bit verbose, and may limit its adoption.
>> > A concise syntax that declares the const property may ease its adoption. For
>> > example: `const var = whatever`
>>
>> For what it's worth, I agree with this.
>>
>
>
> +1

Reply | Threaded
Open this post in threaded view
|

Re: local var <const>

Marcus Mason
In reply to this post by Fangrui Song
This should be how all attributes are specified imo. +1

On Tue, 8 Oct 2019, 11:11 Fangrui Song, <[hidden email]> wrote:
> 3.3.7 – Local Declarations
> ...
> There are two possible attributes: const, which declares a constant variable, that is, a variable that cannot be assigned to after its initialization; ...

local var <const> = whatever

Constant variables help reasoning about the code, but I think the
current syntax is a bit verbose, and may limit its adoption.
A concise syntax that declares the const property may ease its adoption. For
example: `const var = whatever`


Some languages that make declarations of constant variables simple:

OCaml: let foo =     vs    let mut foo =
Scala: val foo =     vs    var foo     =
Rust:  let x   =     vs    let mut x   =