Home > Backend Development > Golang > How Does the Tilde (~) Token Work in Go Generics to Define Underlying Types?

How Does the Tilde (~) Token Work in Go Generics to Define Underlying Types?

Susan Sarandon
Release: 2024-12-13 02:16:10
Original
673 people have browsed it

How Does the Tilde (~) Token Work in Go Generics to Define Underlying Types?

Understanding the Tilde Token (~) in Go Generics

In Go generics, the tilde token (~) plays a crucial role in defining the underlying type of a given type. It operates in the form ~T, indicating the collection of types that share T as their underlying type.

This concept is particularly useful in cases where you want to specify a constraint that allows for types that are derived from a specific underlying type. For instance, consider the following interface constraint:

type Ordered interface {
    Integer | Float | ~string
}
Copy after login

In this example, the constraint defines an interface that can accept any type that is either an integer, a float, or a type whose underlying type is a string. This means that types like MyString, which defines a custom string type, can also satisfy this constraint as long as their underlying type remains string.

Underlying Types in Go

The term "underlying type" refers to the fundamental type that underlies a given type. In Go, this is determined based on the type declaration. For basic types like int, string, and bool, their underlying type is the type itself. However, for composite types like structs, slices, and interfaces, the underlying type is the type that is referenced in the type declaration.

Example Usage of the Tilde Token

The following code demonstrates the use of the tilde token:

type Foo struct {
    n int
}

type ByteSlice []byte

type MyInt8 int8

type MyString string

func echoExact[T ExactSigned](t T) T {
    // Only allows exact types, excluding MyInt8
}

func echo[T constraints.Signed](t T) T {
    // Allows types with underlying type int8, including MyInt8
}
Copy after login

In this example, ExactSigned uses only exact types, which excludes MyInt8. On the other hand, constraints.Signed allows MyInt8 because it contains approximation elements like ~int8.

Note: Limitations of the Tilde Token

It's important to note that the tilde token cannot be used with type parameters. For example, the following code is invalid:

type AnyApprox[T any] interface {
    ~T
}
Copy after login

The above is the detailed content of How Does the Tilde (~) Token Work in Go Generics to Define Underlying Types?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template