feat: enable Go generics for jsii Go CDK code #4009
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Current situation
In Go, strings are not nullable by default like they are in TypeScript, C# etc.
There's also no pointer literal - you can't do something like
&("string value")
to create a pointer to a string literal, it's required to create a variable instead.While there's discussion on having something built into the language in golang/go#45624 it hasn't concluded with a built-in as part of the language, or a standard library function.
The Go JSII library has worked around this by using the approach of creating functions that turn literals and variables into a pointer. Since Go < 1.18 didn't support generics, there's one for each supported JSII type. This means that you have to think about the type of any variables and use the right conversion function, while you're writing code.
Proposal
In Go 1.18, generics were added. These type parameters can be used to provide a single function that convert literals and variables into a pointer.
We can further restrict the allowed types of
T
by using a constraint.If we adopt the name
V
instead ofPtr
to save a further two characters (I'd still be happy withPtr
,Val
etc. as a name), the previous example can be rewritten as:This means that only one function is used to convert multiple types, and the function name can be a few characters shorter.
For the
V
function, integer literals can not be converted tofloat64
, since the output would be a pointer to the input type, not to the allowedfloat64
type. Developers would be prompted that integer types are not in the set of allowed types (string
,float64
,bool
,time.Time
).However, the
Number
function can be updated in place to use generics to accept any numeric type, which simplifies code that uses integer values from:Impact
Since Go 1.18 is already required by the CDK, the use of generics is possible.
For backwards compatibility with existing code, the existing functions would be maintained, so no code would break.
Before
After
Considerations
The name
V
is the shortest, but doesn't describe what it does.The Go team might, at some point, introduce a change to automatically convert literals into pointers, in which case the
jsii.V
andjsii.String
function calls would be redundant and could be stripped out of a codebase. However, this change doesn't make that potential outcome any worse.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.