Make bound funcs consistent with func objects

Propose new features and changes
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Make bound funcs consistent with func objects

29 Jun 2017, 04:45

By your own admittance, that isn't the only use of IsFunc. It is not acceptable to break scripts that rely on previously documented behaviour just because they're in the minority.
We could avoid breaking older script if we make boundfuncs completely consistent with func objects in every aspect. Also this is a reaction towards all your arguments Lexikos.
Recommends AHK Studio
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Make isFunc return 1 for bound funcs

29 Jun 2017, 13:57

just me wrote:Topic: Make isFunc return 1 for bound funcs

My questions: So why should IsFunc() return True? How would you use such a return value?

Your answer: :?:

Code: Select all

mc1 := new MyClass(Func("Foo"))
mc2 := new MyClass(Func("Foo").Bind(1))

Foo(optional := 0){
	[...]
}

Class MyClass {
	__New(callback){
		if (IsFunc(callback)){
			this.callback := callback
		} else {
			msgbox Callback is not a Func object or BoundFunc
			ExitApp
		}
	}
}
Sure, it could be done some other way, but for me, IsFunc returning true if a variable is callable would be the most logical.
If writing a library that accepts callbacks, a simple, consistent check that a valid callback was passed would be nice IMHO.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Make bound funcs consistent with func objects

29 Jun 2017, 16:12

@evilC. I agree that identifying callables is desirable in many cases, but I disagree with using isFunc for it. Here is a perfectly valid use of isFunc, which would break if boundFuncs returns anything but 0 (or "").

Code: Select all

mc1 := new MyClass("Foo")
mc2 := new MyClass(Func("Foo"))

Foo(ref){
	; [...]
}

Class MyClass {
	__New(callback){
		; A reference to the caller is bound to the callback function.
		if (IsObject(callback) && IsFunc(callback))
			this.callback := callback.bind(this)
		else if isFunc(callback)
			this.callback := func(callback).bind(this)
		else
			throw
	}
}
Imo, it would be safer to have abs(callable) return -1, any script that risk passing a callable to abs is already broken anyways. To me, isCallable() returning true if a variable is callable would be the most logical.

Cheers
Spoiler
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Make bound funcs consistent with func objects

29 Jun 2017, 16:27

Helgef wrote:@evilC. I agree that identifying callables is desirable in many cases, but I disagree with using isFunc for it. Here is a perfectly valid use of isFunc, which would break if boundFuncs returns anything but 0 (or "").

Code: Select all

mc1 := new MyClass("Foo")
mc2 := new MyClass(Func("Foo"))

Foo(ref){
	; [...]
}

Class MyClass {
	__New(callback){
		; A reference to the caller is bound to the callback function.
		if (IsObject(callback) && IsFunc(callback))
			this.callback := callback.bind(this)
		else if isFunc(callback)
			this.callback := func(callback).bind(this)
		else
			throw
	}
}
Why would that cause an issue if boundFuncs were completely consistent with func Objects?
Recommends AHK Studio
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Make bound funcs consistent with func objects

30 Jun 2017, 01:17

nnnik wrote:Why would that cause an issue if boundFuncs were completely consistent with func Objects?
So you think that BoundFuncObject.Bind(Value) should be valid?
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Make bound funcs consistent with func objects

30 Jun 2017, 04:00

just me wrote:
nnnik wrote:Why would that cause an issue if boundFuncs were completely consistent with func Objects?
So you think that BoundFuncObject.Bind(Value) should be valid?
Yeah it should be valid for consistency. It will return another bound func with an additionaly parameter in it's array.
Thats what complete consistency means just me.
Recommends AHK Studio
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Make bound funcs consistent with func objects

30 Jun 2017, 05:27

So you want to bind additional parameters to a function object whitout knowing if and how many parameters are already bound? Good luck, nnnik!
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Make bound funcs consistent with func objects

30 Jun 2017, 05:43

just me wrote:So you want to bind additional parameters to a function object whitout knowing if and how many parameters are already bound? Good luck, nnnik!
I don't need to know how many parameters are bound. I just need to know that the next parameters I bind can be bound ( .minParameters/.maxParameters ).
Why would it make a difference if parameters are already bound. If you program properly it shouldn't matter.
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Make bound funcs consistent with func objects

30 Jun 2017, 05:43

No let me correct that in all cases it shouldn't matter
Recommends AHK Studio
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Make bound funcs consistent with func objects

30 Jun 2017, 07:02

I'd say it should matter in every case. If you create a BoundFunc object with bound parameters, you can rely on the fact that these parameters are passed to the function as the first parameters. This order will be changed if you add additional parameters afterwards.
nnnik wrote:I don't need to know how many parameters are bound. I just need to know that the next parameters I bind can be bound ( .minParameters/.maxParameters ).
MinParams() and MaxParams() don't take account of pre-bound parameters.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Make bound funcs consistent with func objects

30 Jun 2017, 10:20

just me wrote:
nnnik wrote:I don't need to know how many parameters are bound. I just need to know that the next parameters I bind can be bound ( .minParameters/.maxParameters ).
MinParams() and MaxParams() don't take account of pre-bound parameters.
You misunderstand. I'm saying that in my implementation it should respect this exactly:
Let me reexplin how I see bound funcs. Towards me bound funcs are just like functions.

Code: Select all

Message( title, msg, exitApp = 0 )
{
	Msgbox % "title: " . title . "`nmsg: " . msg 
	if ( exitApp )
		ExitApp, % exitApp
}

func( "Message" ).bind( "Test Script" )
Is just the same as:

Code: Select all

Message( title, msg, exitApp = 0 )
{
	Msgbox % "title: " . title . "`nmsg: " . msg
	if ( exitApp )
		ExitApp, % exitApp
}

Message.bind( "Test Script" )( msg, exitApp = 0 )
{
	title := "Test Script"
	Msgbox % "title: " . title . "`nmsg: " . msg 
}
So acting upon the func object of the function named Message.bind( "Test Script" ) will result in the new parameter being bound towards the msg.
I don't understand how the new parameters that will be added are not added as the first parameters, though the function that was explicitly passed towards the .bind function?
Could you give an example that shows what you mean?
Recommends AHK Studio
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Make bound funcs consistent with func objects

30 Jun 2017, 11:24

Honestly, I don't understand what you mean.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Make bound funcs consistent with func objects

01 Jul 2017, 02:17

nnnik wrote:Why would that cause an issue if boundFuncs were completely consistent with func Objects?
Since you ask I assume your definition of consistency implies there wouldn't be a problem :thumbup:
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Make bound funcs consistent with func objects

01 Jul 2017, 03:23

To help me understand your concept, would you please answer the following questions?

Code: Select all

; --------------------------------------------------------------------------------------------------------------------------------
; Func object
FO := Func("Function")
MsgBox, 0, Func Object, % "MinParams: " . FO.MinParams . " - MaxParams: " . FO.MaxParams ; 2 - 2
FO.Call("FO Title", "FO Text") ; -> FO Title - FO Text
; --------------------------------------------------------------------------------------------------------------------------------
; BoundFunc object 1
BFO1 := FO.Bind("BFO1 Title")
; The following doesn't work currently, what should be returned?
MsgBox, 0, BoundFunc Object 1, % "MinParams: " . BFO1.MinParams . " - MaxParams: " . BFO1.MaxParams
BFO1.Call("BF01 Text") ; -> BFO1 Title - BFO1 Text
; --------------------------------------------------------------------------------------------------------------------------------
; BoundFunc object 2 (doesn't work currently)
BFO2 := BFO1.Bind("BFO2 Title")
; What should be returned?
MsgBox, 0, BoundFunc Object 2, % "MinParams: " . BFO2.MinParams . " - MaxParams: " . BFO2.MaxParams
; How should BFO2 be called?
BFO2.Call("???")
; Which parameters in which order would be passed to the function?
ExitApp

Function(Title, Text) {
   MsgBox, 0, %A_ThisFunc%, Title: %Title% - Text: %Text%
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Make bound funcs consistent with func objects

01 Jul 2017, 03:37

just me wrote:To help me understand your concept, would you please answer the following questions?
Yeah I will edit the answers into your code:

Code: Select all

[code]; --------------------------------------------------------------------------------------------------------------------------------
; Func object
FO := Func("Function")
MsgBox, 0, Func Object, % "MinParams: " . FO.MinParams . " - MaxParams: " . FO.MaxParams ; 2 - 2
FO.Call("FO Title", "FO Text") ; -> FO Title - FO Text
; --------------------------------------------------------------------------------------------------------------------------------
; BoundFunc object 1
BFO1 := FO.Bind("BFO1 Title")
; The following doesn't work currently, what should be returned?
MsgBox, 0, BoundFunc Object 1, % "MinParams: " . BFO1.MinParams . " - MaxParams: " . BFO1.MaxParams ; 1 - 1
BFO1.Call("BF01 Text") ; -> BFO1 Title - BFO1 Text
; --------------------------------------------------------------------------------------------------------------------------------
; BoundFunc object 2 (doesn't work currently)
BFO2 := BFO1.Bind("BFO2 Text")
; What should be returned?
MsgBox, 0, BoundFunc Object 2, % "MinParams: " . BFO2.MinParams . " - MaxParams: " . BFO2.MaxParams ; 0 - 0
; How should BFO2 be called?
BFO2.Call()
; Which parameters in which order would be passed to the function?
;BFO1 Title - BFO2 Text
ExitApp

Function(Title, Text) {
   MsgBox, 0, %A_ThisFunc%, Title: %Title% - Text: %Text%
}
[/code]
Recommends AHK Studio
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Make bound funcs consistent with func objects

01 Jul 2017, 03:46

Code: Select all

BFO2 := BFO1.Bind("BFO2 Text")
; What should be returned?
MsgBox, 0, BoundFunc Object 2, % "MinParams: " . BFO2.MinParams . " - MaxParams: " . BFO2.MaxParams ; 0 - 0
; How should BFO2 be called?
BFO2.Call()
; Which parameters in which order would be passed to the function?
;BFO1 Title - BFO2 Text
So BFO2 wouldn't be able to specify an own Title parameter?
How do you know whether pre-bound parameters exist when you create BFO2?
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Make bound funcs consistent with func objects

01 Jul 2017, 04:16

So BFO2 wouldn't be able to specify an own Title parameter?
Yes, BFO2 is the result of binding parameters to the BFO1 bound func. That bound func accepts exactly 1 parameter and that's message. It would be strange if binding would not result in binding this parameter. Just like you can't set your own title when calling BFO1 you can't set your own title when binding it.
How do you know whether pre-bound parameters exist when you create BFO2?
I don't understand why that would matter, but I guess it doesn't hurt to disclose the array of bound parameters someway ( e. g. boundFunc.boundParams )

Also it seems towards me that you want to bind parameters to the base func ( FO ). Why would you pass a BFO1 object towards .bind when you want to do that?
Recommends AHK Studio
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Make bound funcs consistent with func objects

01 Jul 2017, 04:30

Following your concept

Code: Select all

BFO2 := BFO1.Bind("BFO2 Text")
will pass "BFO2 Text" as the first parameter (Title) if BFO1 is a Func object. If it is a BoundFunc object, it will be added to the end of the list of pre-bound parameters.

It makes no sense without knowing the correct type of BFO1. Also, boundFunc.boundParams would not be consistent with the current Func objects and require to change your scipts.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Make bound funcs consistent with func objects

01 Jul 2017, 04:59

just me wrote:Following your concept

Code: Select all

BFO2 := BFO1.Bind("BFO2 Text")
will pass "BFO2 Text" as the first parameter (Title) if BFO1 is a Func object. If it is a BoundFunc object, it will be added to the end of the list of pre-bound parameters.
We are currently at an interface layer that is completely abstract from the final implementation. We need to stay on this layer. In the final implementation we could just store the new parameters that were bound but instead keep a reference towards the func object or bound func object that was used to create this bound func.
It makes no sense without knowing the correct type of BFO1.
Over the most recent posts I have asked you several times to find a reason for this.
Also, boundFunc.boundParams would not be consistent with the current Func objects and require to change your scipts.
Why?
Recommends AHK Studio
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Make bound funcs consistent with func objects

01 Jul 2017, 05:31

After you changed the topic we are talking about "Make bound funcs consistent with func objects". I read this as "Let the BoundFunc object support the methods/properties of the current Func object". But now it seems that you want to change the implementation of Func objects to include the functions of BoundFunc objects, do you? I still don't get what you want to achieve with the changes and why you think it wouldn't require changes in existing scripts.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 19 guests