Why is array not truthy or falsy? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
william_ahk
Posts: 482
Joined: 03 Dec 2018, 20:02

Why is array not truthy or falsy?

Post by william_ahk » 27 Jan 2022, 01:52

Code: Select all

arr := []
msgbox % !arr ; 0
msgbox % arr == false ; 0
arr := [1]
msgbox % arr == true ; also 0
Is the only way to know if an array is empty is to check its length?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Why is array not truthy or falsy?  Topic is solved

Post by swagfag » 27 Jan 2022, 02:18

because ur using case sensitive string comparisons. otherwise objects are truthy, regardless of whether they contain anything

the way to know if an array is empty is not to check Length, but to check Count(if ure using v1)

william_ahk
Posts: 482
Joined: 03 Dec 2018, 20:02

Re: Why is array not truthy or falsy?

Post by william_ahk » 27 Jan 2022, 05:20

Ok, so objects are always truthy.
What's the difference between Length and Count (for an array)? Is Count more efficient?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Why is array not truthy or falsy?

Post by swagfag » 27 Jan 2022, 06:46

for the semantic differences, we have: the docs https://www.autohotkey.com/docs/objects/Object.htm#MinMaxIndex
as for whether its more efficient, i dont know. one calls a method and the other doesnt call a method but has to evaluate 2 ternaries. i doubt ud be able to tell which is slower in realworld test cases. in synthetic benchmarks, there might be a difference

Post Reply

Return to “Ask for Help (v1)”