Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upProposal: Add a new type-safe tag system #27597
Comments
gopherbot
added this to the Proposal milestone
Sep 10, 2018
gopherbot
added
the
Proposal
label
Sep 10, 2018
theduke
changed the title from
Proposal: Add typed struct tags
to
Proposal: Add new type-safe tag system
Sep 10, 2018
theduke
changed the title from
Proposal: Add new type-safe tag system
to
Proposal: Add a new type-safe tag system
Sep 10, 2018
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
theduke
commented
Sep 10, 2018
•
|
I just found this proposal: #23637 Which is very similar to this one. |
mvdan
added
LanguageChange
Go2
labels
Sep 11, 2018
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
theduke
commented
Sep 12, 2018
|
Yeah I think this can be closed. |
theduke
closed this
Sep 12, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
theduke commentedSep 10, 2018
•
edited
EDIT: this is very similar to #23637, so maybe it should be closed.
There is recent discussion around Go2 and some larger language changes.
With this, I thought about how one of the brittle and cumbersome features of Go might be improved upon, namely: struct tags.
A note beforehand: this is not a fleshed out proposal and I'm not involved with Go development and have no knowledge about constraints or limitations for a possible implementation. So this is from the perspective of the end-user.
I'm sure I've missed a lot of potential problems, but I'm these will turn up in discussion.
Problem statement
What are the problems with struct tags:
Tags are none the less a very useful feature and important for the ecosystem.
So the question is, how can the situation be improved?
Design Overview
I propose a new tagging system that allows typed tagging of both structs and struct fields.
It is designed to:
A simple example of how the system would work:
So, it works like this:
@pkg.Fn(...)notationImplementation
I think this could be implemented in a backwards compatible way and with a relatively low amount of work.
The
@symbol is currently forbidden as a hard compile time error, so it can be used for this purposeTags are just regular functions and function invocations, so apart from the lexer/parser/AST changes this should be relatively straight-forward to implement. The is no complicated logic necessary.
Tag functions must be executed on package initialization and the values stored in the static type information available to the reflection system
The reflect API must be extended to allow access to the tag information.
reflect API
A rough draft of the changes to the reflect API:
Summary
This proposal suggests a new, backwards compatible tagging system for both structs and and struct fields, allowing for type safe annotation.
This would make the tag system considerably more convenient and safe to use, while not requiring a large amount of language changes.