On Sat, Jul 4, 2020 at 5:50 PM <lx.gu...@gmail.com> wrote:

> Both
>
> type X int | string
>
> and
>
> type X interface int, string
>
> Are meant to be a syntax sugar for:
>
> type X interface {
>  type int, string
> }
>
> It is not a sum type, but rather a generic type that needs to be
> instantiated before the use. That is why it cannot have a zero value:
>
> var x X // error, X must be instantiated before the use
>
> But i think one should not be able to write:
>
> type X interface {
>  type int
>  String() string
> }
>
> But instead it must be something like this:
>
> type X interface {
>  type int, fmt.Stringer
> }
>
> Or:
>
> type X interface int, fmt.Stringer
>
> Or:
>
> type X interface int, interface{String() string}
>
> https://go2goplay.golang.org/p/fNqNeDyM3R9
>
> I do understand that this is kinda the same case like x.Name (complex
> literals in contracts), but in my opinion it would bit a bit more
> consistent syntax, wouldn't it?
>

Not really more consistent. Consider:

type I interface {
    type A, B
    C
}

The constraint this represents could be logically expressed as (type A |
type B) & (interface C), where type constraints require the underlying
types to match, and interface constraints require the interface to be
satisfied. By writing this as interface A, B, C, you're erasing both the
distinction between a type constraint and an interface constraint, as well
as the distinction between X & Y and X | Y. That would make your
alternative syntax *inconsistent* to my mind.

>

-- 
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/CANjmGJtdtXoPQhXxkeFrKpTc-jNTFZAEttnq0rK54%2BPRJ7Nj9g%40mail.gmail.com.

Reply via email to