passing parameters as strings v. numeric Topic is solved

Discuss the future of the AutoHotkey language
User avatar
jeeswg
Posts: 4605
Joined: 19 Dec 2016, 01:58
Location: UK

passing parameters as strings v. numeric

21 Mar 2018, 17:43

- I noticed that when using numbers obtained via SubStr/StrSplit/RegExMatch objects etc, the numbers don't work with SendMessage unless '+0' is used. This would most likely be to do with type (Integer/String).
- Are there any other functions that work like SendMessage?
- I kept having surprising bugs in a script I was working on, and it was this issue each time. A classic example is when you parse an options parameter (a space/tab-separated list).

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by jeeswg on 22 Mar 2018, 10:50, edited 1 time in total.
User avatar
kczx3
Posts: 578
Joined: 06 Oct 2015, 21:39

Re: passing parameters as strings v. numeric

21 Mar 2018, 19:02

I don’t really understand why people continue to pass delimited strings as options to functions. Define an object structure and pass that instead. Issue gone.
User avatar
kczx3
Posts: 578
Joined: 06 Oct 2015, 21:39

Re: passing parameters as strings v. numeric

21 Mar 2018, 19:03

Maybe that’s just the JavaScript coming out in me
User avatar
Flipeador
Posts: 883
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: passing parameters as strings v. numeric

21 Mar 2018, 19:46

Is not this the same as what happens with VarOrAddress in NumPut/NumGet? That's why I always use DllCall instead of SendMessage/PostMessage, it's very unserious to let AHK do magic and guess the data type you're try to pass/return. The same with RegisterCallback.
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2
Helgef
Posts: 3033
Joined: 17 Jul 2016, 01:02
Contact:

Re: passing parameters as strings v. numeric

22 Mar 2018, 01:39

It is not the same as Numxxx var + 0 in v1. Call a string function, get a string. It is good.

kczx3 :thumbup: .

Cheers.
guest3456
Posts: 2332
Joined: 09 Oct 2013, 10:31

Re: passing parameters as strings v. numeric

22 Mar 2018, 09:04

jeeswg wrote:- I noticed that when using numbers obtained via SubStr/StrSplit/RegExMatch objects etc, the numbers don't work


as said, you're not obtaining 'numbers' from those functions, you're obtaining strings.

and so:

https://autohotkey.com/v2/v2-changes.htm wrote:Integer strings on v1 are always converted to pure/binary integers, even if that causes loss of data. For example, the following conversions are performed on v1, but on v2 the string is preserved:
..


always consult the v2-changes doc

User avatar
jeeswg
Posts: 4605
Joined: 19 Dec 2016, 01:58
Location: UK

Re: passing parameters as strings v. numeric

22 Mar 2018, 10:54

- For parameters that expect numbers, it makes sense to be able to handle strings that contain numbers. The function would do '+0' internally.
- Note: SubStr can handle string to numeric.
- Note: InStr can do the reverse, it can convert numeric parameters to strings.

Code: [Select all] [Download] GeSHi © Codebox Plus

vNum := "3"
MsgBox(SubStr("abcde", 3)) ;cde
MsgBox(SubStr("abcde", "3")) ;cde
MsgBox(SubStr("abcde", vNum)) ;cde

vNeedle := 3
MsgBox(InStr("012345", 3)) ;4
MsgBox(InStr("012345", "3")) ;4
MsgBox(InStr("012345", vNeedle)) ;4
Last edited by jeeswg on 22 Mar 2018, 10:58, edited 1 time in total.
guest3456
Posts: 2332
Joined: 09 Oct 2013, 10:31

Re: passing parameters as strings v. numeric

22 Mar 2018, 10:58

jeeswg wrote:For parameters that expect numbers, it makes sense to be able to handle strings that contain numbers.


does SendMessage Wparam and Lparams always expect numbers?

User avatar
jeeswg
Posts: 4605
Joined: 19 Dec 2016, 01:58
Location: UK

Re: passing parameters as strings v. numeric

22 Mar 2018, 11:03

- Numbers are always expected (in AHK v1), as with the SendMessage Winapi function. In AHK v1 the parameters were 'can be an expression', which applies to parameters that are only ever numeric.
- [EDIT: In AHK v1, the parameters are listed as 'can be an expression' (always numeric), however, lower down this is contradicted, you can use "Text". 'A quoted/literal string may also be sent as in the following working example'. Plus there are other nuances/differences in AHK v1/v2 behaviour.]
- Ah, it says here, in line with what Flipeador said. I had thought before it would be good to get rid of this behaviour for NumGet/NumPut, and the same would apply to PostMessage/SendMessage. AFAICS we would be promoting bad practices if we supported it. The behaviour is too quirky, reminiscent of AHK v1, and would probably be more likely to catch people out, and would be unlikely to ever be useful.
- Anyone have any arguments in favour? Are any other functions affected?
PostMessage / SendMessage
https://lexikos.github.io/v2/docs/commands/PostMessage.htm
Each parameter can be a pure integer or a string. If it is a string, the address of the string is sent (even if it is a numeric string). If it is a variable reference such as myBuf and that variable contains a string (or is empty), the variable's length is updated afterward in case the target window modified it.
Last edited by jeeswg on 05 Apr 2018, 18:49, edited 2 times in total.
Helgef
Posts: 3033
Joined: 17 Jul 2016, 01:02
Contact:

Re: passing parameters as strings v. numeric

22 Mar 2018, 13:58

- Numbers are always expected
You say it with certainty, in the same post where you quote the documentation stating the opposite :facepalm:.
User avatar
jeeswg
Posts: 4605
Joined: 19 Dec 2016, 01:58
Location: UK

Re: passing parameters as strings v. numeric

22 Mar 2018, 14:12

@Helgef: What do you mean? The documentation states that it looks for a number, but that it has a weird way of looking for a number.
Helgef
Posts: 3033
Joined: 17 Jul 2016, 01:02
Contact:

Re: passing parameters as strings v. numeric

22 Mar 2018, 15:58

@ jeeswg, I do not know how to respond further, I do not understand what is weird or unclear.
@ guest3456, your v2-changes quote relates to object keys.

Cheers.
guest3456
Posts: 2332
Joined: 09 Oct 2013, 10:31

Re: passing parameters as strings v. numeric

22 Mar 2018, 20:09

jeeswg wrote:@Helgef: What do you mean?


here's what he means:

jeeswg wrote:- Numbers are always expected, as with the SendMessage Winapi function.

...

PostMessage / SendMessage
https://lexikos.github.io/v2/docs/commands/PostMessage.htm
Each parameter can be a pure integer or a string.


contradiction.

further, the very next sentence in the docs explains this entire thread:

https://lexikos.github.io/v2/docs/commands/PostMessage.htm wrote:If it is a string, the address of the string is sent (even if it is a numeric string).
Last edited by guest3456 on 22 Mar 2018, 20:18, edited 1 time in total.

guest3456
Posts: 2332
Joined: 09 Oct 2013, 10:31

Re: passing parameters as strings v. numeric

22 Mar 2018, 20:12

Helgef wrote:@ guest3456, your v2-changes quote relates to object keys.


ah yes

User avatar
jeeswg
Posts: 4605
Joined: 19 Dec 2016, 01:58
Location: UK

Re: passing parameters as strings v. numeric

22 Mar 2018, 21:04

- If any confusion has arisen, it's because of this question. To which the answer is: they expect numeric data in AHK v1, and they ultimately acquire numeric data in AHK v2.
does SendMessage Wparam and Lparams always expect numbers?

- Anyway, I'd be tempted to do away with complex parameters in NumGet/NumPut/oFile.RawRead/oFile.RawWrite/SendMessage/PostMessage, *any others*? I suppose they are being maintained for backwards compatibility/convenience, however, by that same logic, perhaps the 'lesser of two evils' choice for SendMessage/PostMessage would be a different behaviour to the current one ... a dilemma. Otherwise, to be safe, I'd have to use '+0' every time I use SendMessage, or, I'd have to always use a custom SendMessage function.
- (I might propose NumGet/NumPut to work like StrGet/StrPut (although this would be break scripts that don't use the &).)
- So, initially I thought this was a bug, but, since it isn't, any views on what to do would be interesting.
just me
Posts: 5425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: passing parameters as strings v. numeric

23 Mar 2018, 04:47

jeeswg wrote:- If any confusion has arisen, it's because of this question. To which the answer is: they expect numeric data in AHK v1, ...
Really?
Post/SendMessage (v1) wrote:A quoted/literal string may also be sent as in the following working example (the & operator should not be used in this case): ...

Otherwise, to be safe, I'd have to use '+0' every time I use SendMessage, ...
You must not do it if the variable contains a non-numeric string or if you explicitely need to pass a numeric string.
Helgef
Posts: 3033
Joined: 17 Jul 2016, 01:02
Contact:

Re: passing parameters as strings v. numeric

23 Mar 2018, 06:37

- If any confusion has arisen
...it is because of your bad coding practices (gently hinted by kczx3), and reluctance to read the documentation before posting (proved by the existens of this topic).

Otherwise, to be safe, I'd have to use '+0' every time I use SendMessage
That line of thinking is not safe.

guest3456 wrote:contradiction
Indeed.

Cheers.
guest3456
Posts: 2332
Joined: 09 Oct 2013, 10:31

Re: passing parameters as strings v. numeric

23 Mar 2018, 09:11

well said Helgef

User avatar
jeeswg
Posts: 4605
Joined: 19 Dec 2016, 01:58
Location: UK

Re: passing parameters as strings v. numeric

23 Mar 2018, 09:35

- @guest3456/Helgef: I tried to help people gently, to take a hint. I did not contradict myself. And I try to avoid unhelpful statements like that about other people, and to give them the benefit of the doubt ...
- I answered a question that I felt was unclear, that I wanted further clarity on. Before having a chance to criticise the question, which I avoided doing out of politeness, my answer was being (unfairly) criticised.
- I made a statement that was correct, since it was hard to be sure what the question wanted, and to avoid any misunderstandings re. 'numbers'/'numeric' etc, I provided further details underneath.

- @Helgef: So that you know, I was tired trying to resolve the problems over a long period, and did not remotely suspect the cause, and also I didn't know whether one or many functions were affected.
- Breaking the principle of least astonishment/surprise, is one of the biggest dangers in a function/programming language. Sometimes, if you suspect that the majority of users will fall into the trap, serious discussion is needed, re. the language itself and/or the documentation.
- It's common in AHK for parameters that expect string data, to handle numeric types as strings, and vice versa, and that's a strength of AutoHotkey. Also, introducing greater consistency is an aim and strength of AutoHotkey v2.
- Even if the parameters were to maintain their current behaviour, I find the documentation unclear, even now, after multiple readings.

- Further to what just me said. We know now, in this thread, what the problems are, the question is now whether the behaviour should be different. From the AHK v1 page:
PostMessage / SendMessage
https://autohotkey.com/docs/commands/PostMessage.htm
[a number]
The wParam and lParam parameters should be integers.
...
[a number (a pointer)]
A string may be sent via wParam or lParam by specifying the address of a variable.
...
[arguably shouldn't be allowed, it will send a number (a pointer)]
A quoted/literal string may also be sent as in the following working example (the & operator should not be used in this case):
...
SendMessage, 0xC, 0, "New Notepad Title" ; 0XC is WM_SETTEXT
just me
Posts: 5425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: passing parameters as strings v. numeric

23 Mar 2018, 11:17

v2-changes -> Expressions wrote:Address-of: Since pure numbers and strings are now distinct types, &(var := 123) now returns the address of an int64 instead of the address of a string. For consistency, VarSetCapacity returns 6 (the size of int64 minus a null terminator).

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 3 guests