Advertisement
GroggyOtter

Enumerator Example

Nov 7th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 2.49 KB | Source Code | 0 0
  1. ;=======================
  2. ; Creating a class with a customized enumerator.
  3. ; This will allow the class to be passed to a for-loop.
  4. ; Unlike normal enumerators, this one uses 3 parameters.
  5. ; See the for-loop example below the class.
  6. class test_class {
  7.     ; Define some properties to iterate through.
  8.     static Name := 'GroggyOtter'
  9.     static Age := 100
  10.     static URL := 'www.reddit.com/r/AutoHotkey'
  11.     static SSN := 123456789
  12.    
  13.     ; A for-loop always looks for an __Enum() method.
  14.     ; If it finds it
  15.     static __Enum(num_of_vars) {
  16.         ; Normally, AHK's built-in enumerators only
  17.         ; use 1-2 parameters.  
  18.         ; However, we're going to enforce 3 parameters
  19.         if (num_of_vars != 3)
  20.             throw(Error('3 parameters must be provided.'
  21.                 , A_ThisFunc
  22.                 , 'for key, value, value_type in test_class'))
  23.        
  24.         ; Make a call descriptor object. The assigned
  25.         ; function will be called by the for-loop.
  26.         enum := {call:get_next_prop}
  27.        
  28.         ; Make a list of properties to include.
  29.         ; We skip SSN because we don't want it to
  30.         ; show up while iterating through test_class.
  31.         ; If propreties are listed in reverse order,
  32.         ; .Pop() can be used, which is faster than .RemoveAt(1).
  33.         enum.list := ['URL', 'Age', 'Name']
  34.         ; And we need a reference to the
  35.         ; class so we can get the values
  36.         enum.class := this
  37.        
  38.         ; Return the enumerator to the for-loop.
  39.         ; It can use it to loop through each specified item.
  40.         return enum
  41.        
  42.         ; The function that gets called each
  43.         ; iteration of the for-loop
  44.         ; Normally, only 1-2 parameters are used.
  45.         ; A For-loop can handle up to 19 VarRefs
  46.         ; This example shows how to use three.
  47.         get_next_prop(this, &prop, &value, &v_type) {
  48.             ; When there are no properties left
  49.             if (this.list.Length < 1)
  50.                 ; Return false to indicate none left
  51.                 return 0
  52.             ; Get next property name
  53.             prop := this.list.Pop()
  54.             ; And its value
  55.             value := this.class.%prop%
  56.             ; And its type
  57.             v_type := Type(value)
  58.         }
  59.     }
  60. }
  61.  
  62. ; For-looping through test_class
  63. ; Notice it uses 3 parameter values
  64. ; key, value, and value type
  65. for key, value, value_type in test_class
  66.     MsgBox('Property: ' key
  67.         '`nValue: ' value
  68.         '`nType: ' value_type)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement