Re: [go-nuts] Re: What is the problem of the generic use?

119 views
Skip to first unread message

Axel Wagner

unread,
Nov 12, 2021, 2:04:20 AM11/12/21
to tapi...@gmail.com, golang-nuts
Please ask an actual question, don't just post some code.
FTR the code you posted (with the substitution you suggest) compiles and runs fine using gotip.

On Fri, Nov 12, 2021 at 6:18 AM tapi...@gmail.com <tapi...@gmail.com> wrote:
sorry, a mistake, "int" -> "[]byte".

On Friday, November 12, 2021 at 1:08:50 PM UTC+8 tapi...@gmail.com wrote:

package main

import (
    "fmt"
)

type byteview interface{string | int}

type ByteView[T byteview] struct {v [0]T}

func (bv ByteView[T]) Write(v T) (int, error) {
    return 0, nil
}

type Writer[T byteview] interface {
    Write(bs T)(int, error)
}

type ioWriter = Writer[[]byte] // []byte does not satisfy comparable

func main() {
    fmt.Println("Hello, playground")
}

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5342cd82-a51f-4d9d-af79-d7a5e2a1b663n%40googlegroups.com.
Message has been deleted

Axel Wagner

unread,
Nov 12, 2021, 12:22:27 PM11/12/21
to golang-nuts
Why not? The comment "[]byte does not satisfy comparable" seems irrelevant, as you don't use that constraint anywhere. It seems pretty well-defined, if strange and pointless, code.

On Fri, Nov 12, 2021 at 6:20 PM tapi...@gmail.com <tapi...@gmail.com> wrote:
fine? It should not.

Axel Wagner

unread,
Nov 12, 2021, 12:27:34 PM11/12/21
to golang-nuts
To be clear: You sent a follow-up E-Mail to your original code, to replace `int` with `[]byte`. That substitution is, of course, necessary to make the code compile. So, specifically, this compiles fine:

package main

import (
    "fmt"
)

type byteview interface{string | []byte}


type ByteView[T byteview] struct {v [0]T}

func (bv ByteView[T]) Write(v T) (int, error) {
    return 0, nil
}

type Writer[T byteview] interface {
    Write(bs T)(int, error)
}

type ioWriter = Writer[[]byte]

func main() {
    fmt.Println("Hello, playground")
}
Reply all
Reply to author
Forward
0 new messages