You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Often code is very similar for similar datatypes eg. converting an integer type (int, int8, int16, ...) to a string which we can do with strconv.FormatInt. However that example illustrates in my eye's the biggest shortcoming of Go (definitely given the reflective capabilities of the language): the fact that there is no way for parameters to accept similar types.
Eg. strconv.FormatInt has as signature func(int64, int) string this means that when we want to convert an int16 to a string we first have to convert it to an int64, this is a trivial step but it creates some clutter which quickly starts to make code noisy.
My suggestion
What we could do about this, without (entirely) breaking the type system, is to give a list of types that are accepted and then the type that we are going to use in the function.
Then the definition of strconv.FormatInt could become something similar to this:
(note: if the 'being used' type should be included in the list or not is something that should be specified.)
The compiler should then check if int, int8, int16, int32 and int64 are in fact convertible to int64, then insert the conversion and continue as if i were an int64.
When we come to a function that calls strconv.FormatInt we check if the type is in the list.
Backward compatibility and standard library
Since it's an addition to the type syntax and type system, it doesn't break already existing code.
When using this in the standard library, no already existing behaviour changes or breaks.
Final note
For me this system would solve a lot of verbosity and remove the need for a ton of boilerplate (I tend to write an util function per int-like type to convert it to a string).
The text was updated successfully, but these errors were encountered:
Yeah, sorry, I'm going to close this. The Go1 language was effectively frozen as of 2012-03-28. Only minor consistency fixes and clarifications have been made since. I'll consider this a dup of #15292 which will definitely be considered for any possible Go2.
Problem
Often code is very similar for similar datatypes eg. converting an integer type (
int
,int8
,int16
, ...) to astring
which we can do withstrconv.FormatInt
. However that example illustrates in my eye's the biggest shortcoming of Go (definitely given the reflective capabilities of the language): the fact that there is no way for parameters to accept similar types.Eg.
strconv.FormatInt
has as signaturefunc(int64, int) string
this means that when we want to convert anint16
to a string we first have to convert it to anint64
, this is a trivial step but it creates some clutter which quickly starts to make code noisy.My suggestion
What we could do about this, without (entirely) breaking the type system, is to give a list of types that are accepted and then the type that we are going to use in the function.
Then the definition of
strconv.FormatInt
could become something similar to this:(note: if the 'being used' type should be included in the list or not is something that should be specified.)
The compiler should then check if
int
,int8
,int16
,int32
andint64
are in fact convertible toint64
, then insert the conversion and continue as ifi
were anint64
.When we come to a function that calls
strconv.FormatInt
we check if the type is in the list.Backward compatibility and standard library
Since it's an addition to the type syntax and type system, it doesn't break already existing code.
When using this in the standard library, no already existing behaviour changes or breaks.
Final note
For me this system would solve a lot of verbosity and remove the need for a ton of boilerplate (I tend to write an util function per int-like type to convert it to a string).
The text was updated successfully, but these errors were encountered: