|
Larry Clapp |
at Sep 13, 2012 at 7:42 pm
|
⇧ |
| |
Sort of, but not really. The method sets of "string" and "MyType" would
differ. If you want to pass a MyType to something that expects a string,
convert it.
http://golang.org/ref/spec#Conversions There's probably
little or no run-time cost to this, and this makes your intent explicit.
Say you had
type MyType string
func f1(s string) {
s.f2()
}
func (s string) f2() {}
func (s MyType) f2() {}
and you remembered that f1 called f2, and you called f1 with a MyType, you
might easily assume that f1 would call func (s MyType) f2(). But you'd be
wrong.
-- L
On Thursday, September 13, 2012 2:58:33 PM UTC-4, tomwilde wrote:Hi,
But basically the MyType's memory representation ought to be exactly the
same as string's, right?
The program would never encounter unexpected data hidden beneath a derived
type, it could treat MyType as a string safely.
Am Donnerstag, 13. September 2012 18:29:04 UTC+2 schrieb si guy:
Type safety.
MyType and string are not the same type, the reason that you can assign
"foobar" to a MyType is because it is a literal and I think literals are
untyped in go.
--