The content on this page is written in Chinese, and then traslated into English by machine. More accurate traslations are welcome at: https://github.com/wa-lang/man/tree/master/en

Ending's law: "Any application that can be compiled to WebAssembly, will be compiled to WebAssembly eventually."

7.4. Other features of interface

When declaring an interface, the interface's method set can contain another interface, for example:

type I1 interface {
    f1()
}

type I2 interface {
    I1
    f2()
}

Using this form, the compiler will copy the methods of interface I1 to the method set of I2, which is equivalent to directly adding f1() to the method set of I2.

If the method set of interface I2 is a true superset of the method set of interface I1, we say that "compared with I1, I2 is a small interface". This statement seems a bit counter-intuitive, but its internal logic is: the interface is For method contracts, since there are more methods in I2, the set of types that satisfy I2 must be a subset of the set of types that satisfy I1. From this perspective, the empty interface (interface{}) is the largest interface. The more methods contained in a non-empty interface, the smaller the interface tends to be.

We can also declare anonymous interface values. Anonymous interfaces are similar to anonymous structures in all aspects. They are located in the global namespace (can be used across modules).

Interface calls are slightly slower than direct calls from concrete types, so there is no need to create an interface for behavior (or set of methods) that only one type satisfies.

Interface values are comparable. Two non-nil interface values i1 and i2 are equal only if the following conditions are met (that is, the expression i1==i2 is true):

  • The concrete type of i1 is the same as the concrete type of i2, and the types are comparable
  • The concrete value of i1 is equal to the concrete value of i2