Newtype is a expression like
type MyInt int
It's not the same type as int, so why it's permitted?
In Go1 you must perform explicit conversion.
For example,
type Float interface {
type float32, float64
}
func NewtonSqrt(type T Float)(v T) T {
var iterations int
switch (interface{})(v).(type) {
case float32:
iterations = 4
case float64:
iterations = 5
default:
panic("unreachable") // float cannot be nil, any other types are prohibited
by type checker
}
// Code omitted.
}
type MyFloat float32
var myFloatValue MyFloat = MyFloat(64)
var G = NewtonSqrt(float32(myFloatValue)) // MyFloat is converted to
float32 explicitly
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/f1dbb9dc-0620-48e4-94e4-240b84f00caen%40googlegroups.com.