GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing go.dev — Goodbye core types - Hello Go as we know and love it! - The Go Programming Language The Go BlogGoodbye core types - Hello Go as we know and love it!
Robert Griesemer The Go 1.18 release introduced generics and with that a number of new features, including type parameters, type constraints, and new concepts such as type sets. It also introduced the notion of a core type. While the former provide concrete new functionality, a core type is an abstract construct that was introduced for expediency and to simplify dealing with generic operands (operands whose types are type parameters). In the Go compiler, code that in the past relied on the underlying type of an operand, now instead had to call a function computing the operand’s core type. In the language spec, in many places we just needed to replace “underlying type” with “core type”. What’s not to like? Quite a few things, as it turns out! To understand how we got here, it’s useful to briefly revisit how type parameters and type constraints work. Type parameters and type constraintsA type parameter is a placeholder for a future type argument; it acts like a type variable whose value is known at compile time, similar to how a named constant stands for a number, string, or bool whose value is known at compile time. Like ordinary variables, type parameters have a type. That type is described by their type constraint which determines what operations are permitted on operands whose type is the respective type parameter. Any concrete type that instantiates a type parameter must satisfy the type parameter’s constraint. This ensures that an operand whose type is a type parameter possesses all of the respective type constraint’s properties, no matter what concrete type is used to instantiate the type parameter. In Go, type constraints are described through a mixture of method and type requirements which together define a type set: this is the set of all the types that satisfy all the requirements. Go uses a generalized form of interfaces for this purpose. An interface enumerates a set of methods and types, and the type set described by such an interface consists of all the types that implement those methods and that are included in the enumerated types. For instance, the type set described by the interface
consists of all the types whose representation is With this we can now write down the rules that govern operations on generic operands.
For instance, the rules for index expressions state that (among other things)
for an operand
These rules make it possible to index the generic variable
The indexing operation Core typesThis type set-based approach is very flexible and in line with the intentions of the original generics proposal: an operation involving operands of generic type should be valid if it is valid for any type permitted by the respective type constraint. To simplify matters with respect to the implementation, knowing that we would be able to relax rules later, this approach was not chosen universally. Instead, for instance, for Send statements, the spec states that
These rules are based on the notion of a core type which is defined roughly as follows:
For instance, There are various problems with this approach:
Go 1.25For the upcoming Go 1.25 release (August 2025) we decided to remove the notion of core types from the language spec in favor of explicit (and equivalent!) prose where needed. This has multiple benefits:
The respective proposal issue #70128 was recently approved and the relevant changes are already implemented. Concretely this means that a lot of prose in the language spec was reverted to its original, pre-generics form, and new paragraphs were added where needed to explain the rules as they pertain to generic operands. Importantly, no behavior was changed. The entire section on core types was removed. The compiler’s error messages were updated to not mention “core type” anymore, and in many cases error messages are now more specific by pointing out exactly which type in a type set is causing a problem. Here is a sample of the changes made. For the built-in function
A reader who simply wanted to know how
This is shorter and easier to understand. Only when the reader is dealing with a generic operand will they have to contemplate the newly added paragraph:
We made similar changes to each place that mentioned core types. In summary, although this spec update does not affect any current Go program, it opens the door to future language improvements while making the language as it is today easier to learn and its spec simpler.
Next article: More predictable benchmarking with testing.B.Loop |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |