unexpected return values using StrReplace as a BoundFunc object

Get help with using AutoHotkey and its commands and hotkeys
A_AhkUser
Posts: 785
Joined: 06 Mar 2017, 16:18
Location: France

unexpected return values using StrReplace as a BoundFunc object

06 Feb 2018, 20:52

I fail to understand why all these MsgBoxes don't show the same value respectively. Can someone shed a light on this? It seems to occur only if Needle does not appear in Haystack.

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



Thank you.
User avatar
jeeswg
Posts: 4506
Joined: 19 Dec 2016, 01:58
Location: UK

Re: unexpected return values using StrReplace as a BoundFunc object

06 Feb 2018, 21:27

- It looks bug-like. I got the same results, although different unexpected characters. By putting in a go-between function, I didn't get any errors. (The correct result is 'Haystack', the string should remain unchanged as it does not contain 'z'.)

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

- I searched my txt copy of the documentation, and there weren't any examples of binding the func and calling it, in a one-liner. All of the examples did it in two stages, across two lines. However, users might expect that this would be doable.
A_AhkUser
Posts: 785
Joined: 06 Mar 2017, 16:18
Location: France

Re: unexpected return values using StrReplace as a BoundFunc object

06 Feb 2018, 22:30

Hi jeeswg,

I haven't thought about wrapping the command into a wrapper to see if it concerns only built-in functions, thanks for tightening the problem;)
I searched my txt copy of the documentation, and there weren't any examples of binding the func and calling it, in a one-liner. All of the examples did it in two stages, across two lines.


Cheers, good to know. But I agree with you: users might expect that this would be doable.Btw, I use a lot this kind of one-line syntax and never had such unexpected results.

Since it is question of object syntax and since the use of parentheses and the order of all respective evaluations seems to modulate the behaviour, I first took a look @ objects (general information) in the documentation. However, the only remark I found in this respect concerns the hybrid usage of the dot and the brackets (Objects known limitation).
Helgef
Posts: 2990
Joined: 17 Jul 2016, 01:02
Contact:

Re: unexpected return values using StrReplace as a BoundFunc object

07 Feb 2018, 07:39

I will guess that it is related to the boundfunc being released after the expression is evaluated, since no replacement occurs, the function (regexreplace) probably returns the (address of the) input string, which is also released when the boundfunc is. So the command (msgbox) is passed an invalid pointer. To illustrate,

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

MsgBox % Func("RegExReplace").bind("Haystack", "z", "ReplaceText").call() == "Haystack"	; The left hand side of == isn't released before the comparison has been evaluated.
MsgBox % Func("RegExReplace").bind("Haystack", "z", "ReplaceText").call()

If a replacement occurs, or if you assign the result to a variable (within the expression), or if you return the result from a user defined function, the result is copied to a new buffer, which has nothing to do with the boundfunc, hence there is no problem in those cases. /end guessing

Although my imagination cannot come up with any (sensible) real use case when this would be a problem, this topic should probably be moved to bug reports for professional assessment.

Cheers.

v2
A_AhkUser
Posts: 785
Joined: 06 Mar 2017, 16:18
Location: France

Re: unexpected return values using StrReplace as a BoundFunc object

22 Apr 2018, 14:34

Hi everyone,

jeeswg wrote:Bump.

Yeah, I'm sorry for the delayed response...

@Helgef your explanation is convincing.

Helgef wrote:Although my imagination cannot come up with any (sensible) real use case when this would be a problem, this topic should probably be moved to bug reports for professional assessment.

Done. I agree with you. In particular, without claiming that everything shoud be retroactive from v2 to v1, it works on v2.

best regards,

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 9 guests