typeutil

package
v0.0.0-...-f54e8e0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: MPL-2.0, BSD-3-Clause Imports: 6 Imported by: 0

README

typeutil - patched versions of go/types.Identical and golang.org/x/tools/go/type/typeutil.Map

typeutil contains patched version of some Go utilities to handle github.com/cosmos72/gomacro/go/types.Type

  1. an Identical() function with a stricter definition of type identity:

    • interfaces are considered identical only if they print equally, so embedding an interface is different from copying its methods (standard go/types.Identical intentionally does not distinguish these two cases). Also, the order of methods and embedded interfaces is relevant.

    • methods are considered identical only if their receiver, parameters and results types are identical (standard go/types.Identical intentionally ignores the receiver type)

  2. Map: a mapping from go/types.Type to interface{} values, using the stricter definition of type identity defined above.

    Since github.com/cosmos72/gomacro/go/types.Type are not canonical, i.e. not unique, comparing them with == does not give the expected results, as explained in https://github.com/golang/example/tree/master/gotypes#types

    So a specialized map is needed to use them as keys - either golang.org/x/tools/go/type/typeutil.Map, or this patched version github.com/cosmos72/gomacro/go/typeutil/Map, or something analogous

They are useful as type canonicalizing tools for the Go interpreter gomacro, and not necessarily suitable for other purpouses.

License

BSD-3-Clause as the original, unpatched utilities.

Documentation

Overview

Package typeutil defines various utilities for types, such as Map, a mapping from types.Type to interface{} values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Identical

func Identical(x, y types.Type) bool

Identical reports whether x and y are identical.

func IdenticalIgnoreTags

func IdenticalIgnoreTags(x, y types.Type) bool

IdenticalIgnoreTags reports whether x and y are identical if tags are ignored.

func String

func String(x types.Type) string

String returns a string representation of given type, including its receiver if present.

func String2

func String2(name string, x types.Type) string

Types

type Hasher

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

A Hasher maps each type to its hash value. For efficiency, a hasher uses memoization; thus its memory footprint grows monotonically over time. Hashers are not thread-safe. Hashers have reference semantics. Call MakeHasher to create a Hasher.

func MakeHasher

func MakeHasher() Hasher

MakeHasher returns a new Hasher instance.

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 interface{} 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, read-only empty map.

Not thread-safe.

func (*Map) At

func (m *Map) At(key types.Type) interface{}

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) Hasher

func (m *Map) Hasher() Hasher

Hasher gets the hasher used by Map, if present.

All Hashers are functionally equivalent but contain internal state used to cache the results of hashing previously seen types.

A single Hasher created by MakeHasher() may be shared among many Maps. This is recommended if the instances have many keys in common, as it will amortize the cost of hash computation.

A Hasher may grow without bound as new types are seen. Even when a type is deleted from the map, the Hasher never shrinks, since other types in the map may reference the deleted type indirectly.

Hashers are not thread-safe, and read-only operations such as Map.Lookup require updates to the hasher, so a full Mutex lock (not a read-lock) is require around all Map operations if a shared hasher is accessed from multiple threads.

func (*Map) Iterate

func (m *Map) Iterate(f func(key types.Type, value interface{}))

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 interface{}) (prev interface{})

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

func (*Map) SetHasher

func (m *Map) SetHasher(hasher Hasher)

SetHasher sets the hasher used by Map.

All Hashers are functionally equivalent but contain internal state used to cache the results of hashing previously seen types.

A single Hasher created by MakeHasher() may be shared among many Maps. This is recommended if the instances have many keys in common, as it will amortize the cost of hash computation.

A Hasher may grow without bound as new types are seen. Even when a type is deleted from the map, the Hasher never shrinks, since other types in the map may reference the deleted type indirectly.

Hashers are not thread-safe, and read-only operations such as Map.Lookup require updates to the hasher, so a full Mutex lock (not a read-lock) is require around all Map operations if a shared hasher is accessed from multiple threads.

If SetHasher is not called, the Map will create a private hasher at the first call to Insert.

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.

func (*Map) Values

func (m *Map) Values() []interface{}

Values returns a new slice containing the set of map values. The order is unspecified.

Jump to

Keyboard shortcuts

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