proposal: spec: enable conditional type narrowing based on constraint satisfaction for type parameters #73067
Closed
2 of 4 tasks
Labels
LanguageChange
Suggested changes to the Go language
LanguageChangeReview
Discussed by language change review committee
LanguageProposal
Issues describing a requested change to the Go language specification.
Proposal
Milestone
Go Programming Experience
Novice
Other Languages Experience
Python, Java, JS
Related Idea
Has this idea, or one like it, been proposed before?
No
Does this affect error handling?
No
Is this about generics?
Yes, this proposal is fundamentally about enhancing the Go generics system introduced in Go 1.18. It relates directly to the accepted design by proposing added flexibility within generic functions. Currently, the compiler's understanding of a type parameter
T
is fixed by its declared constraint throughout the function body. This proposal suggests allowing the compiler's understanding ofT
's capabilities to be conditionally refined within specific code blocks based on checks against stricter constraints.Proposal
1. What is the proposed change?
We propose introducing a language mechanism that allows developers to check if a type parameter
T
satisfies a specific constraintC
(whereC
might be stricter than the constraintT
was originally defined with). Crucially, if this check succeeds, within the subsequent code block (e.g., theif
block), the compiler should statically recognize thatT
satisfies constraintC
. This enables the direct use of functions, methods, or operations that require constraintC
on values of typeT
, without needing further runtime type assertions or switches for dispatch within that block.Consider the motivating example: writing a function
getComparator[T any]()
that returnscmp.Compare[T]
ifT
happens to satisfycmp.Ordered
, andnil
otherwise.Current Limitation:
The current workaround requires runtime checks and often leads to verbose type switches inside closures, losing some of the elegance and static safety benefits of generics.
Proposed Semantics (Syntax TBD):
We need a way to express the conditional check and the resulting type narrowing. Hypothetical syntax examples:
The precise syntax is secondary to the core semantic proposal: a mechanism to conditionally narrow the effective constraint of a type parameter within a limited scope, based on a check, thereby informing the compiler's static analysis within that scope.
2. Who does this proposal help, and why?
This proposal helps developers writing generic Go code, particularly those creating:
cmp.Ordered
vs. requiring a custom comparator).T
satisfies specific constraints beyond the basic requirements.Why it helps:
any
conversions and runtime assertions which can fail.cmp.Compare[T]
) instead of potentially slower runtime dispatch mechanisms.Language Spec Changes
No response
Informal Change
No response
Is this change backward compatible?
Yes, this proposal is intended to be backward compatible with Go 1. It introduces a new feature or syntax. Existing code that does not use this feature would remain unaffected in compilation and behavior.
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
No response
Cost Description
No response
Changes to Go ToolChain
No response
Performance Costs
No response
Prototype
No response
The text was updated successfully, but these errors were encountered: