Amber icon indicating copy to clipboard operation
Amber copied to clipboard

feature: add constant declarations

Open b1ek opened this issue 1 year ago • 10 comments

i've added support for the const X = Y expression itself. it will resolve to declare -r X=Y, for both local and global variables, since declare -r is the same for locals as well

i also set args in main(args) to be constant. function parameters are never constant.

i don't know which is the minimal bash version for declare -r

b1ek avatar Aug 25 '24 07:08 b1ek

i just checked, declare -r works for bash 4.0:

screenshot that demonstrates how it works in bash 4.0

b1ek avatar Aug 25 '24 07:08 b1ek

The reason why I never introduces const is because sh does not support it. So for instance on ash it fails:

image

Ph0enixKM avatar Sep 05 '24 11:09 Ph0enixKM

When we start to support sh - we should compile it to the regular variable instead (but still never let reassign the variable in the script)

Ph0enixKM avatar Sep 05 '24 11:09 Ph0enixKM

When we start to support sh - we should compile it to the regular variable instead (but still never let reassign the variable in the script)

i'd rather have it defined as a function that always returns the same literal

b1ek avatar Sep 05 '24 12:09 b1ek

The idea of a function that returns always the same value I think that is the best and assure that can't change.

Mte90 avatar Sep 05 '24 13:09 Mte90

I'd say go with the @Ph0enixKM idea, since functions have an additional performance penalty. It doesn't matter that a constant is technically a regular variable, so long as it is translated from an Amber constant, thus is proven to be impossible to modify.

mks-h avatar Sep 08 '24 09:09 mks-h

I think that we can move on with a function that return always the same value so we can assure the best compatibility. In the future we can think for something more sophisticated.

Mte90 avatar Sep 09 '24 08:09 Mte90

In order to resolve this discussion I've created the following poll: https://github.com/amber-lang/amber/discussions/493

Ph0enixKM avatar Sep 27 '24 17:09 Ph0enixKM

@Ph0enixKM im confused, though. afaik we don't support sh right now. why is it an issue?

b1ek avatar Sep 30 '24 11:09 b1ek

I think that we eventually will support sh. I just wanted to settle down how are we going to solve this problem holistically (including sh even if we do not implement it's support just yet)

Ph0enixKM avatar Oct 01 '24 08:10 Ph0enixKM

I think that we'll have to write a guideline on writing error messages. But that's not a hurry. Let's merge the feature

Ph0enixKM avatar Nov 20 '24 10:11 Ph0enixKM

@b1ek can you fix the conflicts so we can merge it?

Mte90 avatar Nov 20 '24 10:11 Mte90