[Fuction] Something interesting, fake enumerated type

Post your working scripts, libraries and tools for AHK v1.1 and older
jj4156
Posts: 19
Joined: 17 Jun 2019, 07:03
Contact:

[Fuction] Something interesting, fake enumerated type

Post by jj4156 » 21 Nov 2020, 13:10

A very simple fake typescript-style enum.

Code: Select all

enum(byref name, items*)
{
	local
	name := {}
	for index, item in items
		--index, name[name[index . ""] := item] := index+0
}
Example:

Code: Select all

enum( Boolean
   , "false"
   , "true"
   , "unknown")
; Generate an associative array named Boolean: {"false": 0, "true": 1, "unknown": 2, "0": "false", "1": "true", "2": "unknown"}
Msgbox, % "Boolean.false is " Boolean.false
; => Boolean.false is 0
May be a useful feature, lol. And only 7 lines
User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: [Fuction] Something interesting, fake enumerated type

Post by rommmcek » 25 Nov 2020, 11:19

I'm a poor mathematician, so can't imagen where to use this, however it's a great exemple for using the same memory address, variadic parameters and nested arrays.

P.s.: It seems to me, the last line of the function could be still more compact: name[name[item]:= --index]:= item.
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: [Fuction] Something interesting, fake enumerated type

Post by kczx3 » 25 Nov 2020, 11:57

Why not return the array? Why use ByRef?
jj4156
Posts: 19
Joined: 17 Jun 2019, 07:03
Contact:

Re: [Fuction] Something interesting, fake enumerated type

Post by jj4156 » 30 Nov 2020, 11:08

kczx3 wrote:
25 Nov 2020, 11:57
Why not return the array? Why use ByRef?
Because I want it to be something that likes a defination. ByRef makes it liking a defination of type enum class.
Post Reply

Return to “Scripts and Functions (v1)”