"Missing" vs empty-string variadic parameter?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

"Missing" vs empty-string variadic parameter?

Post by sirksel » 10 Jun 2022, 12:49

Assuming f(x*) is a variadic function, is there any way to distinguish between a empty string passed vs. a "missing" value. In other words, is there a way to tell the difference between the second and third parameters of f(10,,"",40)?

User avatar
Onimuru
Posts: 107
Joined: 08 Sep 2018, 18:35
Contact:

Re: "Missing" vs empty-string variadic parameter?

Post by Onimuru » 10 Jun 2022, 13:57

You can use

Code: Select all

IsSet()

Code: Select all

f(x*) {
	for v in x {
		if (!IsSet(v)) {
			MsgBox("Null.")
		}
		else if (v == "") {
			MsgBox("Empty string.")
		}
	}
}

sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Re: "Missing" vs empty-string variadic parameter?

Post by sirksel » 10 Jun 2022, 15:53

Thanks for the quick reply.

I thought I tried IsSet() initially, but since I couldn't perform it on array element reference like x[2] (which I know is a property resolution, not a variable), I figured I was doing it wrong. In my actual code I need to reference certain param array elements by index number in a loop, so I can't use for..in. I thought maybe doing this might work -- I made g(x*) mirroring your f(x*):

Code: Select all

f(x*) {
  for v in x
    if (!IsSet(v))
      MsgBox("Null.")
    else if (v == "")
      MsgBox("Empty string.")
}
g(x*) {
  loop x.length
    if (v:=x[a_index], !IsSet(v))
      MsgBox("Null.")
    else if (x[a_index] == "")
      MsgBox("Empty string.")
}
f(10,,'',40)
g(10,,'',40)
It doesn't work though. So maybe I need to clarify my question:
Short of iterating the variadic param array with a for loop, is there a way to test if a particular array element (for which you know the index) is unset?

sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Re: "Missing" vs empty-string variadic parameter?

Post by sirksel » 10 Jun 2022, 16:04

I think I might have figured it out.
Array.Prototype.Has()

Code: Select all

g(x*) {
  loop x.length 
    if (!x.has(a_index))
      MsgBox("Null.")
    else if (x[a_index] == "")
      MsgBox("Empty string.")
}
g(10,,'',40)
There's just a missing index #2, right? It's kind of weird because since for..in works, I would think that it does have an index #2. Maybe it has the index, but the index doesn't have a value in that position?

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: "Missing" vs empty-string variadic parameter?

Post by lexikos » 10 Jun 2022, 22:01

Yes, Has is for Array what IsSet is for variables.
Has
Returns true if the specified index is valid and there is a value at that position, otherwise false.
Length
The length includes elements which have no value. Increasing the length changes which indices are considered valid, but the new elements have no value (as indicated by Has).
The enumeration behavour of Array might not be immediately obvious, but...
For Index, Value in ArrayObj
...
Index
The array index, typically the same as A_Index.
Value
The value (if there is no value, Value becomes uninitialized).

Source: Array Object - Methods & Properties | AutoHotkey v2
It is specifically designed to iterate over every position, not only populated elements.

An array of length 2 always has an index #2, but it might not have a value at that position. Likewise, with for v in x, the variable v always exists, but it doesn't always have a value.
v2.0-a125-5f342996
Changed Array enumerator to convert missing items to unset variables instead of empty strings.
Source: AutoHotkey v2 alpha (UPDATES) - Page 4 - AutoHotkey Community
Maybe it would be better for arr[n] to throw when !arr.Has(n), but it might be a bit late to make that change. I was basically just aiming for parity between what built-in functions can do with parameter lists and what variadic functions can do.

Concerning implementation details, it could also be said that an index is never had by an Array. You pass an index, the Array calculates the address of the element based on the index and stores or retrieves the value. The index is just a parameter and is never stored in the array. The array is just a contiguous sequence of elements, and each element may have a value or not.

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: "Missing" vs empty-string variadic parameter?

Post by Helgef » 21 Jun 2022, 13:26

lexikos wrote: Maybe it would be better for arr[n] to throw when !arr.Has(n), but it might be a bit late to make that change.
I do not think it is too late, :arrow: Array.__item does not define what happens when !arr.Has(n).

I think it would have been helpful :arrow: in this case.

Cheers.

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: "Missing" vs empty-string variadic parameter?

Post by lexikos » 01 Jul 2022, 20:55

v2.0-beta.6 changes arr[n] to throw an UnsetItemError when !arr.Has(n) (unless n is out of range, in which case it throws an IndexError).

A future version might allow arr[n] ?? value.

Post Reply

Return to “Ask for Help (v2)”