BoundFunc has no name

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Mattia72
Posts: 4
Joined: 01 Dec 2023, 02:35
Contact:

BoundFunc has no name

Post by Mattia72 » 01 Dec 2023, 02:51

[Moderator's note: Topic moved from Bug Reports.]

If I create a FuncObj with Bind Method, it doesn't get a name.

Code: Select all

DoNothing(msg) {
}

fn := DoNothing.Bind('something')
if (fn.Name = '')
  throw Error('DoNothing has no name')

; But so works:
fo := DoNothing
fn := fo.Bind('something')
if (fn.Name = '')
  throw Error('DoNothing has no name')

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

Re: BoundFunc has no name

Post by iseahound » 05 Dec 2023, 04:31

When you bind an argument to a function, it should have a different name.

Code: Select all

; Assume there is a function called Max(x, y) which returnss the greater of x and y.

; We now can create a greater than or equal to function.
greaterThan100 := Max.Bind(100)

; Same as: (y >= 100) ? y : 100
MsgBox greaterThan100(200) ; True (200)
MsgBox greaterThan100(50) ; False (100)

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

Re: BoundFunc has no name

Post by lexikos » 08 Dec 2023, 22:09

fn is not DoNothing. Does the documentation give you reason to believe that it should have the same name?
Other properties and methods are inherited from Func, but do not reflect the properties of the target function or method (which is not required to be implemented as a function). The BoundFunc acts as an anonymous variadic function with no other formal parameters, similar to the fat arrow function below:

Code: Select all

Func_Bind(fn, bound_args*) {
    return (args*) => (args.InsertAt(1, bound_args*), fn(args*))
}
Source: Function Objects - Definition & Usage | AutoHotkey v2

Mattia72
Posts: 4
Joined: 01 Dec 2023, 02:35
Contact:

Re: BoundFunc has no name

Post by Mattia72 » 09 Dec 2023, 14:34

Ok, but I think that is a little bit strange that there is difference between the two constructs:

Code: Select all

DoNothing(msg) {
}
fn := DoNothing.Bind('something') ; no name
and

Code: Select all

fo := DoNothing
fn := fo.Bind('something') ; name=DoNothing
If I want to call a Bound Function by name, I have to choose the second one and not to forget, not to refactor it later. ;)

niCode
Posts: 327
Joined: 17 Oct 2022, 22:09

Re: BoundFunc has no name

Post by niCode » 09 Dec 2023, 23:55

Unless the latest version of 2.1 has changed something, then neither of the examples you gave are correct for me. fn.Name is blank every time.

Mattia72
Posts: 4
Joined: 01 Dec 2023, 02:35
Contact:

Re: BoundFunc has no name

Post by Mattia72 » 10 Dec 2023, 11:24

You're right, I overlooked something :oops:

Mattia72
Posts: 4
Joined: 01 Dec 2023, 02:35
Contact:

Re: BoundFunc has no name

Post by Mattia72 » 10 Dec 2023, 12:43

In this example, I commented the real state.

Code: Select all

DoNothing(msg) {
  MsgBox(msg)
}

fn1 := DoNothing.Bind('something') ; no name
fo := DoNothing                             ; fo.Name = DoNothing
fn2 := fo.Bind('something')             ; no name

fn2.Call()                                     ; ok
%fo.Name%.Call("something")        ; ok

%fn1.Name%.Call()                       ; doesn't work
%fn2.Name%.Call()                       ; doesn't work

niCode
Posts: 327
Joined: 17 Oct 2022, 22:09

Re: BoundFunc has no name

Post by niCode » 10 Dec 2023, 15:11

How would you call this separate line of code here:

Code: Select all

() => MsgBox('I am an anonymous fat-arrow function')
Without a reference, you can't. It's anonymous, it has no name.

When you use .Bind(), you're essentially creating a new function, one that has no name. It may reference DoNothing, but it is not DoNothing.



I'd be curious to know the purpose of this:

Code: Select all

%fn1.Name%.Call()
If fn1 is already a reference to a BoundFunc, then call fn1. You don't need to know the name.

Post Reply

Return to “Ask for Help (v2)”