I have the following code-snippet:
type F func()
type I interface {}
func A() {}
func B() {}
func test() {
var a interface{} = A
var b interface{} = B
if A == B { // 1. Compile error
// Code
}
if a == b { // 2. No compile error
// Code
}
}
If functions are not comparable and interfaces are comparable, why can I assign a function to an interface type?
--
To clarify my question another code-snippet:
type I interface {
DoSomething()
}
type F func()
func (f F) DoSomething() {
f()
}
func A() {
fmt.Println("A")
}
func B() {
fmt.Println("B")
}
func test() {
var _a F = A
var _b F = B
var a I = _a
var b I = _b
if a == b { // 2. No compile error but panic
// Code
}
}
It seems to me, that I can break the type system with simple assignments.
I do not propose that functions should be comparable. My questions are:
- Why interfaces are comparable by default?
- Why is it not possible to mark an interface as comparable resp. not comparable?
- Why is it not possible do define user defined-equality?
- What is the motivation in this language design?