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."

6.4. Embedded structure

When declaring a structure type, if the type of a member is a structure but the name of it is omitted, this usage is called embedded structure. For example, in the following code, the structure Sc is embedded The Sp member:

type Sp struct {
    x: i32
}

type Sc struct {
    Sp // embedded structure
    y: i32
}

The member name of the embedded structure is its type name, and we can still use the selector . to access it, such as the following printing code:

    v: Sc
    println(v.Sp.x)

In this example, you can even omit the .Sp part. For example, the above code is equivalent to the following code:

    v: Sc
    println(v.x)

In this usage, the structure Sp appears to be embedded in the structure Sc, which is where the name embedded structure comes from. However, if the structure contains a member with the same name as the embedded structure, it cannot be omitted when accessing the member with the same name of the embedded structure, for example:

// 版权 @2023 凹语言 作者。保留所有权利。

type Sp struct {
    x: i32
}

type Sc2 strct {
    Sp
    x: f32
}

func main(){
    v: Sc2
    println(v.x)    // Print Sc2.x, its type is f32
    println(v.Sp.x) // Print Sc2.Sp.x, its type is i32
}

In addition to being able to reuse the data layout of a type, another important function of an embedded structure is that it can reuse type methods. The structure will automatically have the methods of the embedded type, for example:

// 版权 @2023 凹语言 作者。保留所有权利。

type Sp struct {
    x: i32
}

func Sp.Show {
    println(this.x)
}

type Sc struct {
    Sp
    y: i32
}

func main {
    v := Sc{Sp:Sp{x: 42}, y: 13}
    v.Show() // 42
}

When declaring an embedded structure literal, the name of the embedded structure cannot be omitted, such as Sc{Sp:Sp{x: 42}, y: 13} in the above example. If it is omitted like {x: 42, y : 13}, it will be considered illegal.

After Sp is embedded in Sc, Sc obtains the method of Sp, so that the variable v of type Sc can perform the Show operation; in this example, v.Show() is equivalent to v.Sp.Show() . If the structure has a method with the same name as the embedded structure, the processing method is similar to the member with the same name, for example:

// 版权 @2023 凹语言 作者。保留所有权利。

type Sp struct {
    x: i32
}

func Sp.Show {
    println(this.x)
}

type Sc struct {
    Sp
    x: f32
}

func Sc.Show {
    println(this.x)    
}

func main {
    v := Sc{Sp:Sp{x: 42}, x: 13.14}
    v.Show()    // 13.14
    v.Sp.Show() // 42
}

In order to achieve object reuse, Wa-lang does not adopt inheritance (which is different from C++), but uses combination. The embedded structure is a concrete manifestation of combination. The embedded structure reuses the memory layout and method set of the embedded type. Together with the interface (to be introduced in Chapter 7), it constitutes Wa-lang's foundation of abstraction and reusing of objects.