Keyword const

Propose new features and changes
irral
Posts: 6
Joined: 14 May 2019, 02:38

Keyword const

29 Jul 2019, 07:15

const keyword, that define variable as constant.

Code: Select all

const a := "some value"
a := "b" ; throws exception
numput("UInt", some_value, &a) ; throws exception
; --------------
const c  ; like const c := "" or error at the stage of reading the script
; --------------
class A {
	static const b := 2
}
A.b := 3 ; throws exception
; --------------
class B {
	static v := 2
	set(v) {
		this.v := v
	}
}
foo(B)

foo(const o)
{
	o.set(4) ; throws exception
}
swagfag
Posts: 2912
Joined: 11 Jan 2017, 17:59

Re: Keyword const

29 Jul 2019, 07:49

how about mut/var/??? and make all variables const by default
irral
Posts: 6
Joined: 14 May 2019, 02:38

Re: Keyword const

29 Jul 2019, 09:01

swagfag wrote:
29 Jul 2019, 07:49
and make all variables const by default
Sarcasm? I'm talking about those cases when I want to make sure that this variable will not be accidentally changed during the work of the program.
O'kay, I can write the class for that purpose, like this (v2):

Code: Select all

class const {
	static constants := {}
	__Item[name] {
		set {
			if ( this.constants.HasKey(name) )
				throw Exception("Trying to set the value to constant", -1)
			else if ( value == "" )
				throw Exception("Trying to set the empty value to constant", -1)
			
			this.constants.%name% := value
		}
		get => this.constants.%name%
	}
}
but, it will not provide sufficient access protection.
swagfag
Posts: 2912
Joined: 11 Jan 2017, 17:59

Re: Keyword const

29 Jul 2019, 10:43

why would u think it was sarcasm
the suggestion is as follows:
make all variables const by default.

Code: Select all

i := 1 ; this is const
i := 2 ; throw
++i ; throw
introduce a keyword to enable mutability(a reverse-const, if u will)

Code: Select all

var i := 1 ; mutable
i := 2 ; ok
++i ; ok
Helgef
Posts: 3925
Joined: 17 Jul 2016, 01:02
Contact:

Re: Keyword const

29 Jul 2019, 11:46

numput("UInt", some_value, &a) ; throws exception
Do you mean that the address-of operator is prohibited for const
vars? This case for numput is feasible to detect at load time, but that would be very limited protection. For example, I don't think it's feasible to detect if Dllcall will change const values.

I like const too, both for error detection and possibly performance optimisations.

Cheers.
Helgef
Posts: 3925
Joined: 17 Jul 2016, 01:02
Contact:

Re: Keyword const

29 Jul 2019, 12:28

:oops: Good thinking nnnik :thumbsup:.

Cheers.
swagfag
Posts: 2912
Joined: 11 Jan 2017, 17:59

Re: Keyword const

29 Jul 2019, 13:20

hmm BufferAlloc with flags, interesting
irral
Posts: 6
Joined: 14 May 2019, 02:38

Re: Keyword const

29 Jul 2019, 14:46

swagfag wrote:
29 Jul 2019, 10:43
why would u think it was sarcasm
I mean that it's not necessary. Constants are used much less frequently than common variables, therefore, they must have a unique keyword, such as const.
Or, add "const" flag for variable handle, that show possibility to set value to this var in that moment. I think it willn't protect from Numput, but it will have almost no effect on performance and the number of operations performed during the script works.
User avatar
nnnik
Posts: 4253
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Keyword const

30 Jul 2019, 00:53

@irral my suggestion only adds protection from numput/strput/the * operator/dllcall/COM/other processes/Send or Post message.
It is based on the idea that your const keyword will get implemented.
Recommends AHK Studio

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 11 guests