The only real downsides, I think, are that compiler errors gets harder to
read:
https://github.com/golang/go/issues/21866
And likewise, that the name won't appear at runtime, so if you print things
using `reflect`, the result will be less readable.

I think there are cases where it has advantages, such as breaking import
cycles. They probably can be solved in other ways too, though.

IMO, if the conclusion would be "always use type-aliases", I'd prefer "Go
should make parameters and returns contra- and covariant respectively".
Because we'd essentially saying that interfaces should be represented by
their method set, not their type-name:
https://github.com/golang/go/issues/8082
Which is a fine position to take, but full contra- and covariance is both
cleaner and more powerful - again, IMHO.

On Tue, Sep 8, 2020 at 10:14 AM 'Javier Zunzunegui' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> TLDR: Why should I NOT the alias form `type ... = interface {...}` instead
> of the standard `type ... interface {...}` for interface declarations?
>
> The extended question:
>
> The normal type definition for an interface in go would be `type TypeName
> interface {...}`. This results in a new type, which is assignable to other
> types of the similar interfacce, such that this is valid:
> ```
> type I1 interface{ Foo() }
> type I2 interface{ Foo() }
> var _ I1 = I2(nil)
> ```
> But these are different types, such that any function with one of these in
> the signature can't be converted to a similar function with the other, such
> that this is NOT valid:
> ```
> var _ func(I1) = (func(I2))(nil)
> ```
>
> Declare the types as aliases <https://yourbasic.org/golang/type-alias/> 
> however,
> and the limitation dissapears:
> ```
> type I1 interface{ Foo() }
> type I2 interface{ Foo() }
> var _ I1 = I2(nil) // still valid
> var _ func(I1) = (func(I2))(nil) // now valid
> ```
>
> A runnable example of the below using `http.ResponseWriter`:
>
>    - without alias <https://play.golang.org/p/dKdh80LNMt0>
>    - with alias <https://play.golang.org/p/sR5hB0ParA0>
>
> My point being using aliases offers an advantage (more flexible assignment
> and interface implementation) and to my knowledge no meaningful drawback.
> So why not use it everywhere?
>
> --
> 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 golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/d9595614-ce72-4602-8ead-67e9a1427181n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/d9595614-ce72-4602-8ead-67e9a1427181n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHmJeriqzsbYrhyuX%3DNzeMvJ3GcAAoXBwL_TKgkBkPUtg%40mail.gmail.com.

Reply via email to