"Missing" vs empty-string variadic parameter?
"Missing" vs empty-string variadic parameter?
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)?
Re: "Missing" vs empty-string variadic parameter?
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.")
}
}
}
Re: "Missing" vs empty-string variadic parameter?
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*):
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?
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)
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?
Re: "Missing" vs empty-string variadic parameter?
I think I might have figured it out.
Array.Prototype.Has()
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?
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)
Re: "Missing" vs empty-string variadic parameter?
Yes, Has is for Array what IsSet is for variables.
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.
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.
Has
Returns true if the specified index is valid and there is a value at that position, otherwise false.
The enumeration behavour of Array might not be immediately obvious, but...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).
It is specifically designed to iterate over every position, not only populated elements.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
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.
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.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
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.
Re: "Missing" vs empty-string variadic parameter?
I do not think it is too late, Array.__item does not define what happens when !arr.Has(n).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 think it would have been helpful in this case.
Cheers.
Re: "Missing" vs empty-string variadic parameter?
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.
A future version might allow arr[n] ?? value.