Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

enum type and while loop


  • Please log in to reply
7 replies to this topic
trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010
; Enumerate!
enum := obj._NewEnum()
MsgBox % "k=" k " v=" v " enum[k,v]=" enum[k,v] ; "" "" 1
While enum[k, v]
    t .= k "=" v "`n"
msgbox % s

1. How does the while loop know what 'k' and 'v' values are?
2. Is there a special while loop behavior if expression is enumerator object?
3. does enum[k,v] is some kind of an array in an array syntax?
a) generally
B) in context of while loop

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
1: It doesn't.
2 and 3-b: No. While accepts an ordinary expression, and interprets the result as either true or false.
3-a: It is not an array, but it is array-like syntax. However, it can do whatever the object wants it to do. In this case, it is equivalent to enum.Next(k, v).

trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010
Thanks for the response Lexikos.

1: It doesn't.

1. Why doesn't 'k' and 'v' variables equal to "" or 0 inside while loop?
A: bc 'k' and 'v' are initialized by enum[k, v] command (I'm replying to myself).

obj := Object("red", 0xFF0000, "blue", 0x0000FF, "green", 0x00FF00)

; Enumerate!
enum := obj._NewEnum()
MsgBox % "k=" k " v=" v " enum[k,v]=" enum[k,v] ; "" "" 1
	; ""invoking enum[k, v] increases enumerator counter (see while loop)"  - this is false" - no, this is actually true ;)
	; 
	; enum[k, v] initializes k and v variables! (table index passed as byRef?)
msgbox, % k " " v ; "red" 0xFF0000
While enum[k, v]
{
	msgbox, % isObject(k) " " isObject(v)  ; 0 0
	t .= k "=" v "`n"
}
msgbox % t
2. are 'k' and 'v' variables somehow passed 'by ref' into the enum object? (this looks strange to me in comparison to arrays of arrays syntax)

cat := Object("fur", "black", "eyes", "green", "smiles", true)
enum := ObjNewEnum(cat)
Msgbox % cat["fur"] ; black
msgbox % enum[box] ; 1
;msgbox % enum(box) ; call to nonexistent function
msgbox % box ; eyes
3. how to create an object, whose method / constructor will initialize variables through obj[variable] syntax? (like the enumerator object does with 'box' variable)

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
2. enum[k,v] is shorthand for enum.next(k,v)

3.
enum := squares._NewEnum()
while enum[k,v]
	MsgBox %k% squared = %v%

class squares {
	i := 1
	_NewEnum() {
		return new squares
	}
	Next(byref k, byref v) {
		k := this.i
		v := this.i**2
		this.i++
		return true
	}
	__Get(byref k, byref v) {
		return this.next(k,v)
	}
}

esc::ExitApp


trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010

2. enum[k,v] is shorthand for enum.next(k,v)

This fills the hole in my thinking very nicely, thank you!

3.

enum := squares._NewEnum()
while enum[k,v]
	MsgBox %k% squared = %v%

class squares {
	i := 1
	_NewEnum() {
		return new squares
	}
	Next(byref k, byref v) {
		k := this.i
		v := this.i**2
		this.i++
		return true
	}
	__Get(byref k, byref v) {
		return this.next(k,v)
	}
}

esc::ExitApp

Build-in enum methods overloaded inside a class [object]. Splendid :p .

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

2. enum[k,v] is shorthand for enum.next(k,v)

This fills the hole in my thinking very nicely, thank you!

Was my explanation difficult for you to understand?

... it is equivalent to enum.Next(k, v).



trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010

2. enum[k,v] is shorthand for enum.next(k,v)

This fills the hole in my thinking very nicely, thank you!

Was my explanation difficult for you to understand?

... it is equivalent to enum.Next(k, v).

Yes it was. Sometimes, when you respond Lexikos, you're using very many thinking shortcuts to explain the thing. Sometimes, the thing is explained 'too' concisely for the [average] user, and, without code examples, the user fails to understand your post. That's why I've asked additional questions as I simply did not get the whole thing from your post. The post led me in the right direction though, so thanks again for the response.
Don't get me wrong, I really like reading your posts when you respond to people. It was that I've got a problem understanding what you were saying without an example snippet of code. And it was like 4:00 am in the morning :lol:

trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010
Just for the reference.
car := Object("name", "Ferrari"
			, "color", "red"
			, "fuel", 100)
enum := car.NewEnum()

enum.Next(k, v)
enum.Next(k, v)
msg(k " " v)
	; fuel 100

enum[k, v]
msg(k " " v)
	; name Ferrari

; it looks like the enum[k, v] resets the enum enumerator internal counter, as
; enum[k, v] returns "the first" key, value pair of the car object
; This is wrong (see below).

for k, v in car
	s .= k " " v "`n"
msg(s)
	; keys are stored in alphabetical order in ahk object
		; color red
		; fuel 100
		; name Ferrari
	; keys _are not_ stored in initialization order in ahk object
		; name Ferrari
		; color red
		; fuel 100
	; i. e.
enum2 := car.NewEnum()
enum2[k, v]
msg(k " " v)
	; color red != name Ferrari!

; Now that we know that, we see that enum[k, v] is in deed an alias for enum.Next(k, v)
enum3 := car.NewEnum()
enum3.Next(k, v)
enum3[k, v]
enum3.Next(k, v)
	; third key value pair in k, v => name Ferrari
msg(k " " v)
	; name Ferrari


msg(t) {
	msgbox, % t
}