reflect.DeepEqual, compare functions

186 views
Skip to first unread message

Vladimir VG

unread,
Mar 8, 2012, 11:30:14 AM3/8/12
to golan...@googlegroups.com
It returns true only if both funcs are nil.

Okay, we can't determine are two pieces of code equal or not.

Why do not compare pointers of functions?
Why do not return true, if we surely know that in some cases two func values are equal?

Jan Mercl

unread,
Mar 8, 2012, 12:31:54 PM3/8/12
to golan...@googlegroups.com
It used to be like that before. Specs has changed recently (month or two?) and function [pointers] can now be compared only against nil. The rationale, IIRC, was that the old specification ties the hands of compiler in some cases. Not sure about this, but I think some code is generated even at run time - that might be one of the places for which the equality to work make things hard for the compiler and/or requires suboptimal code to be generated. Mostly I'm guessing here, though.

chris dollin

unread,
Mar 8, 2012, 12:44:49 PM3/8/12
to Vladimir VG, golan...@googlegroups.com

To allow the answer to change without affecting the behaviour of
programs.

func outer() func(int)int { return func(x int) { return x + 1 } }

... outer() == outer()

true or false?

func alpha(int x) int { return x + 1 }
func beta(int x) int { return x + 1 }

... alpha == beta

true or false?

If the program is sensitive to the result, people will depend on it
(even if the documentation contains pictures of large red dragons
saying DO NOT DO) and that handicaps the performance-hungry
compiler writer.

Chris

--
Chris "allusive" Dollin

Reply all
Reply to author
Forward
0 new messages