Conversion of type parameters

March 4, 2024

Today I’ll be live-streaming again, doing TDD on an open-source project. I hope you can join!


Conversions

Additionally, if T or x’s type V are type parameters, x can also be converted to type T if one of the following conditions applies:

  • Both V and T are type parameters and a value of each type in V’s type set can be converted to each type in T’s type set.
  • Only V is a type parameter and a value of each type in V’s type set can be converted to T.
  • Only T is a type parameter and x can be converted to each type in T’s type set.

So in other words, conversion to/from a type parameter is not possible if there’s a possibility that the conversion would be impossible.

Let’s illustrate with examples.

func foo[T int | int8, V int | int16](x V) T {
	return T(x) // Allowed, All types of V can be converted to all types of T
}

func foo2[T int | int8, V int | int16 | string](x V) T {
	return T(x) // Not allowed: cannot convert x (variable of type V constrained by int | int16 | string) to type T: cannot convert string (in V) to type int (in T)
}

func bar[V int | int16] (x V) int {
	return int(x) // Allowed: Each type of V can be converted to int
}

func bar2[V int | int16] (x V) bool {
	return bool(x) // Not allowed: cannot convert x (variable of type V constrained by int | int16) to type bool: cannot convert int (in V) to type bool
}

func baz[T int | int8] (x int) T {
	return T(x) // Allowed: x can be converted to each of T's types
}

func baz2[T int | int8 | bool] (x int) T {
	return T(x) // Not allowed: cannot convert x (variable of type int) to type T: cannot convert int to type bool (in T)
}

Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Exact & loose type unification

Type unification … Unification uses a combination of exact and loose unification depending on whether two types have to be identical, assignment-compatible, or only structurally equal. The respective type unification rules are spelled out in detail in the Appendix. The precise definitions of “exact” and “loose” unification are buried in the appendix, and depend on the specific types involved. In general, I think it’s not terribly inaccurate to say that exact unification applies when the two types are identical, for composite types with identical structure (i.


Type unification

A couple of days ago we saw the spec reference the concept of “type unification”. Today we start through that explanation…. Type unification Type inference solves type equations through type unification. Type unification recursively compares the LHS and RHS types of an equation, where either or both types may be or contain bound type parameters, and looks for type arguments for those type parameters such that the LHS and RHS match (become identical or assignment-compatible, depending on context).


Successful type inference

Today we finish up the discussion on type inference. So we’ve now Type inference … If the two phases are successful, type inference determined a type argument for each bound type parameter: Pk ➞ Ak A type argument Ak may be a composite type, containing other bound type parameters Pk as element types (or even be just another bound type parameter). In a process of repeated simplification, the bound type parameters in each type argument are substituted with the respective type arguments for those type parameters until each type argument is free of bound type parameters.