Skip to content

spec: embedding a type alias is confusing #17746

Closed
@alandonovan

Description

@alandonovan

When you embed a type alias within a struct, the resulting struct type has a field of the alias's apparent name, but its run-time type descriptor uses the alias's original name. For example:

package main

import "fmt"
import "bytes"

type buffer => bytes.Buffer

type S struct {
        buffer
}

func main() {
        var s S
        _ = s.buffer
        // _ = s.Buffer // compile error: s has no field Buffer
        fmt.Printf("%#v\n", s) // "main.S{Buffer:bytes.Buffer{...}}"
}

This is potentially confusing to the reader, and I suspect it will break a number of analysis tools that assume the name in the syntax of the embedded field declaration is the same as the name of the field.

Something to think about.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions