On Fri, Feb 11, 2022 at 12:51 PM Kamil Ziemian <kziemian...@gmail.com>
wrote:

> Can someone explain me, why compiler can't throw an error when it find
> unused type parameter? I like that in Go unused variable, or import, is
> compile time error and I would appreciate the same behavior with unused
> type parameters.
>

I'm not sure it's reason enough, but I do have a case where I want to have
unused type-parameters on a type.
Essentially, I have

type ID string
func Call(ctx context.Context, id ID, req Message) (Message, error)

which does an RPC-like call. It's used as

r, err := framework.Call(ctx, somepkg.ID, somepkg,Request{…})
resp := r.(somepkg.Response)

With generics, this would be

type ID string
func Call[Req, Resp Message](context.Context, ID, Req) (Resp, error)

But this requires writing

resp, err := framework.Call[somepkg.Request, somepkg.Response](ctx,
somepkg.ID, somepkg.Request{…})

as return types can't be inferred. Instead, I plan to do

type ID[Req, Resp Message] string // unused type-parameters
func Call[Req, Resp Message](context.Context, ID[Req, Resp], Req) (Resp,
error)

which allows `somepkg` to declare

var ID = framework.ID[MyRequest, MyResponse]("my-name")

letting the client write

resp, err := framework.Call(ctx, somepkg.ID, somepkg.Request)

Now somepkg.ID carries both types and is in an argument, so both types can
be inferred.

As I said, I'm not 100% yet on whether this is a good idea and much less if
that would be enough reason to keep allowing unused type parameters. But
there is at least some potential benefit to allowing it.


>
> I need to go for a while, I will go back with more questions about what
> you can get when using "[]" in Go.
>
> Best,
> Kamil
>
> --
> 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/b085b991-e59e-4225-a4d6-36b2391e1dc2n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/b085b991-e59e-4225-a4d6-36b2391e1dc2n%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/CAEkBMfH83V2aA7mzaLTC_DzrJ1XsRRwYiJJRmLffRKj%2Bn%3DqZ9Q%40mail.gmail.com.

Reply via email to