How to Compare Functions

How to compare functions?

You can use identical:

identical(m,mean)

How to compare 2 functions in Go?

Before going further: you should refactor and not compare function value addresses.

Spec: Comparison operators:

Slice, map, and function values are not comparable. However, as a special case, a slice, map, or function value may be compared to the predeclared identifier nil.

Function values are not comparable. What you may do is compare if the addresses of the function values are the same (not the address of variables holding function values, but the function values themselves).

You can't take the address of a function, but if you print it with the fmt package, it prints its address. So you can use fmt.Sprintf() to get the address of a function value.

See this example (based on your code):

hand := &Handler{Undefined, Defined}
p1 := fmt.Sprintf("%v", Undefined)
p2 := fmt.Sprintf("%v", hand.Get)
fmt.Println("Expecting true:", p1 == p2)

fmt.Println("Expecting false:", fmt.Sprintf("%v", Defined) == fmt.Sprintf("%v", hand.Get))
fmt.Println("Expecting true:", fmt.Sprintf("%v", Defined) == fmt.Sprintf("%v", hand.Post))

Output (try it on the Go Playground):

Expecting true: true
Expecting false: false
Expecting true: true

Another option would be to use reflect.Value.Pointer() to get the address of the function values, this is exactly what the fmt package does: fmt/print.go:

func (p *pp) fmtPointer(value reflect.Value, verb rune) {
// ...
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice,
reflect.UnsafePointer:
u = value.Pointer()
// ...
}

But you should refactor and not compare function value addresses.

What's best practice for comparing function return to a simple data type?

The condition in the if statement

if(x = functReturnsInt() < 5)

is just incorrect. It is equivalent to

if(x = ( functReturnsInt() < 5 ) )

So the best practice is to write

int x = functReturnsInt();
if ( x < 5 ) /*...*/

provided that you need further in the code the value returned from the function.

If the value returned from the function is needed only in an if statement then it is enough to write

if (  functReturnsInt() < 5 )
{
/* ...*/
}
//...

How to compare two functions that are called with `.bind()`?

I think the best solution here is to store a key alongside your callback inside of the array. This is a lot more reliable and way less hacky than any solution comparing functions:

register: function register(callback, key) {
console.log('register callback');
var obj = {
key: key,
callback: callback
}
callbackList.push(obj);
}

you can then call remove by passing the key desired rather than the callback, and you can compare as such:

if(callbackList[0].key === key)

Just make sure you pass the desired key when registering and access the callback property within the array objects where necessary.

I think this solution using labels is much cleaner and doesn't require any confusing or strange code.

how do I compare 2 functions in javascript


var a = b = function( c ){ return c; };
//here, you can use a === b because they're pointing to the same memory and they're the same type

var a = function( c ){ return c; },
b = function( c ){ return c; };
//here you can use that byte-saver Andy E used (which is implicitly converting the function to it's body's text as a String),

''+a == ''+b.

//this is the gist of what is happening behind the scences:

a.toString( ) == b.toString( )

How to compare the signature of two functions?

Essentially you want to check if types of two functions are the same:

std::is_same_v<decltype(funA), decltype(funB)>

I wouldn't call this 'comparing signatures', as, if I remember correctly, return type is not a part of a signature (because it doesn't affect overload resolution).



Related Topics



Leave a reply



Submit