[generics] (again). Type inference is somewhat weak

204 views
Skip to first unread message

Denis Cheremisov

unread,
Jun 17, 2020, 12:43:35 PM6/17/20
to golang-nuts
https://go2goplay.golang.org/p/ObL79WVHDjw

Need to write types explicitly although all the info needed is easily accessible:

fmt.Println(IsOK(*commonResponse, *commonError)(r))

What I am sure is I will keep using a code generator instead of this monstrosity. 

Denis Cheremisov

unread,
Jun 17, 2020, 9:13:54 PM6/17/20
to golang-nuts
Better example. A method to request for exacly one element from the gRPC bistream. A thing that is frequently needed when there're lots of bistream gRPC methods.


IRL I would need to call it as

resp, err := proto.ReadOne(service.Service_MethodClient, service.MethodRequest, service.MethodResponse)(ctx, client.Method, &service.MethodRequest{
    …
})

instead of simple to read

resp, err := proto.ReadOne(ctx, client.Method, &service.MethodRequest{
    …
})

среда, 17 июня 2020 г., 19:43:35 UTC+3 пользователь Denis Cheremisov написал:

roger peppe

unread,
Jun 18, 2020, 3:23:21 AM6/18/20
to Denis Cheremisov, golang-nuts
Yes, I agree. It seems to have trouble inferring types when the argument is a generic interface type.

  cheers,
    rog.

--
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/6bfdcc4c-dca8-4051-b89c-65274c2f6982o%40googlegroups.com.

Ian Lance Taylor

unread,
Jun 19, 2020, 12:34:38 AM6/19/20
to roger peppe, Denis Cheremisov, golang-nuts
On Thu, Jun 18, 2020 at 12:23 AM roger peppe <rogp...@gmail.com> wrote:
>
> Yes, I agree. It seems to have trouble inferring types when the argument is a generic interface type.
> Here's a simpler example: https://go2goplay.golang.org/p/3-aVhD6Y9R2
> I think this is probably https://github.com/golang/go/issues/39661

Is this related to
https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#type-inference-for-generic-function-arguments
?

Ian
Reply all
Reply to author
Forward
0 new messages