Code: Select all
^d::
df := 00
Switch df
{
case "0": ToolTip, 1
case "00": ToolTip, 2
}
Return
Code: Select all
^d::
df := 00
Switch df
{
case "0": ToolTip, 1
case "00": ToolTip, 2
}
Return
Code: Select all
df := 1
Switch df
{
case 0:
ToolTip, 1
Sleep 1000
return
case 1:
ToolTip, 2
Sleep 1000
return
}
Code: Select all
#Requires AutoHotkey v2.0-
^d::
{
df := "00"
Switch df
{
case "0": ToolTip 1 ;AutoHotkey v1.1
case "00": ToolTip 2 ;AutoHotkey v2.0-
}
}
Code: Select all
df := 00
Switch "|" df
{
case "|0": Msgbox, 1
case "|00": Msgbox, 2
}
Return
Oh That works But Why ? How ? what the point ? isnt a bug ?flyingDman wrote: ↑26 Oct 2022, 21:38"Cheap and dirty" workaround:Code: Select all
df := 00 Switch "|" df { case "|0": Msgbox, 1 case "|00": Msgbox, 2 } Return
Code: Select all
df = 00
Switch df
{
case "0": Msgbox, 1
case "00": Msgbox, 2
}
Return
Code: Select all
MsgBox, % 1 = 1 ; True
MsgBox, % 1 + 1 ; 2
MsgBox, % 1 = 01 ; True
MsgBox, % 1 + 01 ; 2
MsgBox, % "1" = 1 ; True
MsgBox, % "1" + 1 ; 11
MsgBox, % "1" = 01 ; False
MsgBox, % "1" + 01 ; 101
For historical reasons, quoted numeric strings such as "123" are always considered non-numeric when used directly in an expression
By contrast, the Switch documentation does not specify how the values are compared (numerically or non-numerically). When the expressions/values are being compared by switch/case, they are not being "used directly in an expression". I wrote more in the other topic.Equal (=), case-sensitive-equal (==), and not-equal (<> or !=). If both inputs are numbers or numeric strings, they are compared numerically; otherwise they are compared alphabetically. ...
Note: In AutoHotkey v1, a quoted string (or the result of concatenating with a quoted string) is never considered numeric when used directly in an expression.
If an operator which can accept either one operand (&x) or two numeric operands (x & y) follows a quoted literal string, auto-concat occurs and the operator is applied only to the right-hand operand. This is because quoted literal strings are always considered non-numeric and are therefore not valid input for numeric operators. For example, expressions like "x" &y and "x" ++y now work.
In AHK, if you use the operand of "1234" directly, it is always a string of characters. However, If you define x := "1234", then x can be a number or a string, depending on context. Am I correct?This is because quoted literal strings are always considered non-numeric
Code: Select all
x := "1"
y := "2"
a := 1
MsgBox, % x + y ; result 3
MsgBox, % "1" + "2" ; result 12
MsgBox, % x = 1 ; result True
MsgBox, % x = "1" ; result True
MsgBox, % x = a ; result True
MsgBox, % a = "1" ; result True
MsgBox, % x + a ; result 2
I wouldn't say that is "the key phrase". It is just explaining the rationale for a syntax exception ("a" + b is always interpreted as "a" . +b). The syntax exception exists regardless of the runtime behaviour or type system.
That is close enough, but be careful of generalizing quirks as they are not always consistent. It is better to understand them in context. Also, I think saying that a value "can be a number" is too inaccurate. In some contexts, a string value can be converted to or evaluated as a number.In AHK, if you use the operand of "1234" directly, it is always a string of characters. However, If you define x := "1234", then x can be a number or a string, depending on context. Am I correct?
For the purpose of deciding whether to compare the values as numbers or numeric strings, a quoted string is never considered numeric. In other words, a quoted string is never compared numerically.Equal (=), case-sensitive-equal (==), and not-equal (<> or !=). If both inputs are numbers or numeric strings, they are compared numerically; otherwise they are compared alphabetically. ...
Note: In AutoHotkey v1, a quoted string (or the result of concatenating with a quoted string) is never considered numeric when used directly in an expression.
So for the purpose of determining whether the inputs are valid for a math operator, a quoted string is always considered invalid, and will cause the result to be "" (in v1).Except where noted below, any blank value (empty string) or non-numeric value involved in a math operation is not assumed to be zero. Instead, it is treated as an error, which causes that part of the expression to evaluate to an empty string. For example, if the variable X is blank, the expression X+1 yields a blank value rather than 1.
For historical reasons, quoted numeric strings such as "123" are always considered non-numeric when used directly in an expression (but not when stored in a variable or returned by a function). This non-numeric attribute is propagated by concatenation, so expressions like "0x" n also produce a non-numeric value (even when n contains valid hexadecimal digits). This problem can be avoided by assigning the value to a variable or passing it through a function like Round(). Scripts should avoid using quote marks around literal numbers, as the behavior may change in a future version.
I do not believe the vagueness and quirks of the v1 "type system" (if it can be said to have one) were motivated by modern language design principles. AutoHotkey started out with syntax like AutoIt v2, no expressions and no type model. Up until (I think) v1.0.48, variables were only able to contain strings. The quirk with quoted strings predates that; it was most likely an optimization measure, avoiding the need to evaluate whether the string is numeric (because quote marks were presumed to show that the author intended it to be non-numeric).I realize that many modern languages have moved toward the loosely typed model,
What does it mean for the mutability to be the same, or for the types to be stronger or weaker? v1 doesn't have a consistent type system in the first place. If you want to know about v2, there is detailed documentation.
In other words, can a string of numbers be treated as a numeric value when use in certain ways?lexikos wrote:What does it mean for the mutability to be the same
I think I answered that withlexikos wrote:or for the types to be stronger or weaker?
Thank you for pointing me to the docs. Does this not seem contradictory to you?RussF wrote: a string is a string and a numeric value is numeric, and never the twain shall meet
and 5 paragraphs below is:...so that str := "123" is always a string and int := 123 is always an integer. Consequently, str needs to be converted every time it is used as a number...
What happened to " "123" is always a string"? How do you compare a string to a binary value? This is what I mean by strong typing (or lack thereof).So for example, 54 and "530" are compared numerically...
It is not. You are likely conflating the value itself with how the value is used. A string is a string, even if you are able to convert it to something else, implicitly or explicitly. Type(str) and Type(int) will tell you what type the value has. Once you convert the value, you may have both the original value and a new value of a different type (or just the latter).
I wrote:Also, I think saying that a value "can be a number" is too inaccurate. In some contexts, a string value can be converted to or evaluated as a number.
No, you did not answer my question before I asked it.I think I answered that with
The most practical way is to parse the string to produce another binary value, then compare the two binary values, although technically in that case the string value is not being compared. If you want to compare a string to a binary value more directly, you can do it by parsing the string and using modulo on the binary value.How do you compare a string to a binary value?
I don't think that's what you mean. What you're not saying is what alternative behaviour you might be expecting; it's not about how you compare values of two different types, but whether such a thing is even allowed and what result the attempt will have.This is what I mean by strong typing (or lack thereof).
Using my example from above:lexikos wrote: What you're not saying is what alternative behaviour you might be expecting
Code: Select all
x := "1"
y := "2"
a := 1
MsgBox, % x + y ; result 3
MsgBox, % "1" + "2" ; result 12
MsgBox, % x = 1 ; result True
MsgBox, % x = "1" ; result True
MsgBox, % x = a ; result True
MsgBox, % a = "1" ; result True
MsgBox, % x + a ; result 2
My point EXACTLY. The fact that AHK implicitly converts it sometimes, but not always, depending on context, can be very confusing. As I stated above, I had to read the docs several times to try and get a handle on when and where these implicit conversions occur. In most other languages, evaluating If x = 1 would produce an error because they are two different types. You would have to use something like If Val(x) = 1, If Val("1") = 1 or If Str(a) = "1", and do the conversion explicitly. Knowing that you HAVE to do the conversion yourself rather than wondering (or looking up) whether you have to, depending on the particular context), is far less confusing to me.lexikos wrote: A string is a string, even if you are able to convert it to something else, implicitly or explicitly.
Have you found any exceptions other than the literal quoted strings, which I have said is specific to v1?Numeric strings: A string of digits (or any other supported number format) is automatically interpreted as a number when a math operation or comparison requires it.
In my experience, implicit conversion is very common in scripting languages.In most other languages, evaluating If x = 1 would produce an error because they are two different types.
You won't get the protection from forgetting to do the conversion, but you're free to do it explicitly.Knowing that you HAVE to do the conversion yourself rather than wondering (or looking up) whether you have to, depending on the particular context), is far less confusing to me.
Some interpretation of values must occur for a comparison to take place. As I said, it is possible to interpret the characters of a digit string while also interpreting the decimal places of a binary number and compare the two, without converting one to the other. So I don't think either point is particularly relevant.So, we agree that a conversion must take place to compare variables or literals of two different types.
I would call that a strongly typed language, which AutoHotkey clearly does not have. There are degrees to everything, so to ask if it is more strongly typed is a different question to whether implicit conversions are allowed.When I refer to "strong" or "loose" typing, I mean that in a stronger typed language, you must do that conversion explicitly
No, not personally. The post that started this thread was what initially brought the issue to my attention. It was new to me, probably because, out of habit, I have always explicitly converted strings when needed and never had any problems.lexikos wrote: Have you found any exceptions other than the literal quoted strings, which I have said is specific to v1?
While I have been writing BAT and CMD scripts ever since the first IBM PC came out in the early '80s, AHK is pretty much the only other scripting language I have used (unless you want to include interpreted BASIC back in the day). All others are compiled or pseudo-compiled.In my experience, implicit conversion is very common in scripting languages.