typeutil

package
v1.20.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2026 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package typeutil defines various utilities for types, such as Map, a hash table that maps types.Type to any value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hasher

type Hasher struct{}

A Hasher provides a Hasher.Hash method to map a type to its hash value. Hashers are stateless, and all are equivalent.

func MakeHasher

func MakeHasher() Hasher

MakeHasher returns Hasher{}. Hashers are stateless; all are equivalent.

func (Hasher) Hash

func (h Hasher) Hash(t types.Type) uint32

Hash computes a hash value for the given type t such that Identical(t, t') => Hash(t) == Hash(t').

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map is a hash-table-based mapping from types (types.Type) to arbitrary values. The concrete types that implement the Type interface are pointers. Since they are not canonicalized, == cannot be used to check for equivalence, and thus we cannot simply use a Go map.

Just as with map[K]V, a nil *Map is a valid empty map.

Read-only map operations (Map.At, Map.Len, and so on) may safely be called concurrently.

TODO(adonovan): deprecate in favor of https://go.dev/issues/69420 and 69559, if the latter proposals for a generic hash-map type and a types.Hash function are accepted.

func (*Map) At

func (m *Map) At(key types.Type) any

At returns the map entry for the given key. The result is nil if the entry is not present.

func (*Map) Delete

func (m *Map) Delete(key types.Type) bool

Delete removes the entry with the given key, if any. It returns true if the entry was found.

func (*Map) Iterate

func (m *Map) Iterate(f func(key types.Type, value any))

Iterate calls function f on each entry in the map in unspecified order.

If f should mutate the map, Iterate provides the same guarantees as Go maps: if f deletes a map entry that Iterate has not yet reached, f will not be invoked for it, but if f inserts a map entry that Iterate has not yet reached, whether or not f will be invoked for it is unspecified.

func (*Map) Keys

func (m *Map) Keys() []types.Type

Keys returns a new slice containing the set of map keys. The order is unspecified.

func (*Map) KeysString

func (m *Map) KeysString() string

KeysString returns a string representation of the map's key set. Order is unspecified.

func (*Map) Len

func (m *Map) Len() int

Len returns the number of map entries.

func (*Map) Set

func (m *Map) Set(key types.Type, value any) (prev any)

Set sets the map entry for key to val, and returns the previous entry, if any.

func (*Map) SetHasher

func (m *Map) SetHasher(Hasher)

SetHasher has no effect.

It is a relic of an optimization that is no longer profitable. Do not use Hasher, MakeHasher, or [SetHasher] in new code.

func (*Map) String

func (m *Map) String() string

String returns a string representation of the map's entries. Values are printed using fmt.Sprintf("%v", v). Order is unspecified.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL