[Fuction] Something interesting, fake enumerated type

Post your working scripts, libraries and tools
jj4156
Posts: 10
Joined: 17 Jun 2019, 07:03

[Fuction] Something interesting, fake enumerated type

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: 1188
Joined: 15 Aug 2014, 15:18

Re: [Fuction] Something interesting, fake enumerated type

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: 1223
Joined: 06 Oct 2015, 21:39

Re: [Fuction] Something interesting, fake enumerated type

25 Nov 2020, 11:57

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

Re: [Fuction] Something interesting, fake enumerated type

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.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: tuzi and 28 guests