3 ms·
> But why (1)? If a `fishTank` is a `container[fish]` But a `fishTank` is not a `container[fish]`. In common parlance type T Thing is newtyping. T has t
by masklinn 5y ago
> But why (1)? If a `fishTank` is a `container[fish]`
But a `fishTank` is not a `container[fish]`.
In common parlance
type T Thing
is newtyping. T has the same implementation as the underlying type, and Go allows conversions back and forth, but they are otherwise unrelated, and the new type does not share the interface (method set) of the original.
It is, in a way, a shorthand for
type T struct { _0 Thing }
- hiptobecubic 5y agoOh I misunderstood `type T Thing` to be an alias and not a haskell-style newtype. $ ghci GHCi, version 8.8.4: https://www.haskell.org/ghc/ :? for help Prelude> class HasField a where { field1 :: a -> Int } Prelude> data Foo = Foo Prelude> instance HasField Foo where { field1 f = 2 } Prelude> field1 Foo 2 Prelude> newtype Bar = Bar Foo Prelude> :t Bar Bar :: Foo -> Bar Prelude> field1 (Bar Foo) <interactive>:7:1: error: • No instance for (HasField Bar) arising from a use of ‘field1’ • In the expression: field1 (Bar Foo) In an equation for ‘it’: it = field1 (Bar Foo) Prelude> type Baz = Foo Prelude> field1 (Foo :: Baz) 2
- morelisp 5y agoThere's an unfortunately-close syntax that does define type aliases in Go, type T = Thing