-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: enable Go generics for jsii Go CDK code #4009
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RomainMuller
suggested changes
Mar 17, 2023
RomainMuller
previously approved these changes
Mar 22, 2023
mergify
bot
dismissed
RomainMuller’s stale review
March 22, 2023 09:04
Pull request has been modified.
RomainMuller
approved these changes
Mar 22, 2023
RomainMuller
added a commit
that referenced
this pull request
Mar 22, 2023
Users can customize the `node` runtime used by the jsii runtime for java by providing the `JSII_NODE` environment variable. Additionally, this corrects how the child process is spawned so that `JSII_NODE` and `JSII_RUNTIME` can contain spaces (previously, this would result in a spawn error). Added a test to verify the various scenarios work as intended. Fixes #4009
Thank you for contributing! ❤️ I will now look into making sure the PR is up-to-date, then proceed to try and merge it! |
Merging (with squash)... |
Merging (with squash)... |
mergify bot
pushed a commit
that referenced
this pull request
Mar 22, 2023
Users can customize the `node` runtime used by the jsii runtime libraries by providing the `JSII_NODE` environment variable. Additionally, this corrects how the child process is spawned in Java so that `JSII_NODE` and `JSII_RUNTIME` can contain spaces (previously, this would result in a spawn error). Added a test to verify the various scenarios work as intended. Fixes #4009 --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.