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
proposal: Go 2: type range #40621
Labels
Milestone
Comments
martisch
added
v2
A language change or incompatible library change
LanguageChange
labels
Aug 6, 2020
Please note that you should fill https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing a language change. |
mvdan
added
the
WaitingForInfo
Issue is not actionable because of missing required information, which needs to be provided.
label
Aug 6, 2020
I think this should be folded into #19412. I think it's the same basic idea. |
|
seankhliao
removed
the
WaitingForInfo
Issue is not actionable because of missing required information, which needs to be provided.
label
Jun 12, 2022
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
The Problem
In go functions capable of accepting different types uses
interface{}
, this work well most of the time but this very unclear about what your function actualy accepts. Also dealing with case when someone pass a wrong type resolve most of the time in panic or error but idealy we want the build the to fail because the function will never work with a wrong type.The Solution :
Allows people to define type range :
A type range is obtained by oring types, the result is a type exposing
interface{}
(and also more less working like it) but only capable to fit the given types :Do achieve a similar result currently you would use
interface{}
:As you can see this is more ambiguous, also there is no good solution for
a
to solve this issue (could be returning with an error or panic but idealy we want this to not build).How to exploit a type range types :
Earlier I've said type range expose
interface{}
so to use it you must cast it :This is still like how we do with
interface{}
but adds more security, trying to cast to a type not in type range errors :Other things :
Type range can be merged and can be used outside of a type declaration, if multiple types overlap the type is only added once :
The order of types doesn't matter (it's just if a type is in or not) and may be changed by the compiler or reflection.
You should be able to create a range with any type (like
interface{}
does), sonet.IP|uint32|[4]byte|[16]byte
would be legal.Why not using a less permissive model for what is exported ? (if every types in your type range have a
String() string
method havingString
be callable in the type range it self)Because this is dangerous, if any one of your types change this breaks your code with a very unhelpfull error message. If you want to do this just use something like
interface{String() string}
for your type.Change in the core libs ? Maybe some method like
strconv.Itoa
or other might be benificial but that not really what I'm up for (plus this can be done later) (plus methods have to manualy deal with type range so this might reduce performance or code readability).Reflection ? I think this would be needed but I'm not experienced enough with
reflect
to propose something. I just know you should be able to test if a Type fit in a type range and extract types of a type range.EDIT:
I forgot 2 things :
You can use a type range to an other type range if all types of the red one fit in the writed one :
If you have an interface in your type range an object fitting the interface also fit the type range :
The text was updated successfully, but these errors were encountered: