(ahk v2-beta.15) Assiging functions to objects behaves wierdly? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Elequosoraptor
Posts: 18
Joined: 11 Oct 2017, 09:04

(ahk v2-beta.15) Assiging functions to objects behaves wierdly?

Post by Elequosoraptor » 12 Nov 2022, 11:21

So I was trying to assign a function to an object literal by just writing it out like so

Code: Select all

testObj := {
    key: "a",
    function: script(x) {
        SendInput("l")
    }
}
But it seems those inner curly braces throw an error, which is bizarre to me. After all this works fine:

Code: Select all

script(x) {
    SendInput("l")
}
testObj := {
    key: "a",
    function: script
}
[Mod edit: [code][/code] tags added.]

I've gone over the docs for objects and functions and can't seem to find why defining an object's properties like this doesn't work. It's definitely the curly braces that are the issue, if you instead do function: (x)=> SendInput("l") it works fine, but obviously that isn't suitable for more complex functions that aren't as simple as a single line.

Thoughts? Crossing my fingers this is a real mystery and not something obvious I missed, but it seems to be straightforwardly weird.

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

Re: (ahk V2.15) Assiging functions to objects behaves wierdly?

Post by swagfag » 12 Nov 2022, 12:51

such syntax is not supported

iseahound
Posts: 1472
Joined: 13 Aug 2016, 21:04
Contact:

Re: (ahk V2.15) Assiging functions to objects behaves wierdly?  Topic is solved

Post by iseahound » 12 Nov 2022, 13:02

For now try:

Code: Select all

testObj := {
    key: "a",
    function: (x) => (
        SendInput("l")
    )
}

Elequosoraptor
Posts: 18
Joined: 11 Oct 2017, 09:04

Re: (ahk V2.15) Assiging functions to objects behaves wierdly?

Post by Elequosoraptor » 12 Nov 2022, 13:11

iseahound wrote:
12 Nov 2022, 13:02
For now try:

Code: Select all

testObj := {
    key: "a",
    function: (x) => (
        SendInput("l")
    )
}
Shame it isn't supported, but your suggestion works! I'm glad I don't have to restructure my script entirely, thanks for the help.

Elequosoraptor
Posts: 18
Joined: 11 Oct 2017, 09:04

Re: (ahk V2.15) Assiging functions to objects behaves wierdly?

Post by Elequosoraptor » 12 Nov 2022, 13:15

That works great for my purposes. Shame it isn't supported, but I really appreciate the suggestion (and can't believe I missed it!)

lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: (ahk v2-beta.15) Assiging functions to objects behaves wierdly?

Post by lexikos » 12 Nov 2022, 17:16

I have corrected your topic title for posterity. You are using v2.0-beta.15, not v2.15, and such a version might exist in future.

Post Reply

Return to “Ask for Help (v2)”