Re: [go-nuts] Interface arguments to generic functions

2021-01-20 Thread Ian Lance Taylor
On Tue, Jan 19, 2021 at 10:02 PM burak serdar  wrote:
>
> As a generic-function writer, I do not know if the argument will be an
> interface or a concrete type. I don't want to check if it is
> zero-value, I want to check if it is nil, because I don't want it to
> panic.

Other people in this thread have made useful comments.  I want to add
that this request doesn't make sense to me.  A generic type has
constraints that describe exactly what operations are permitted for
values of that generic type.  The current proposal provides no way to
write a constraint for "can be compared to nil".  If we could write
such a constraint, then if you used it, that type parameter could not
be instantiated by a numeric type or a string type.  That doesn't seem
clearly useful.

It seems to me that what you are suggesting is something like "if a
type argument is an interface type, then permit comparing with nil."
I don't think that's a useful way to program in a language like Go.
Calling a method on a nil interface value will crash in a reliable and
dependable way, just as dereferencing a nil pointer will crash.  There
is no more need to check for a nil interface value than there is a
need to check for a nil pointer value.

Finally, you can call this if you really must:

func IsNilInterface[T any](v T) bool {
typ := reflect.TypeOf().Elem()
if typ.Kind() != reflect.Interface {
return false
}
return reflect.ValueOf().Elem().IsZero()
}

Ian

-- 
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/CAOyqgcVykAqBefafk%3DUF%3D_1sh0U5tjVy12QbJmOzNnXfNzhe8A%40mail.gmail.com.


Re: [go-nuts] Interface arguments to generic functions

2021-01-19 Thread Patrick Smith
On Tue, Jan 19, 2021 at 7:06 PM burak serdar  wrote:

> However, there is no way for P to check if x is nil. This does not compile:
>
> func P[T fmt.Stringer](x T) {
>  if x!=nil {
> fmt.Println(x)
> }
> }


Is this  doing what you want?

func P[T fmt.Stringer](x T) {
 if fmt.Stringer(x)!=nil {
fmt.Println(x)
}
}

https://go2goplay.golang.org/p/7S4RX5G3rT_3

-- 
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/CAADvV_t6iLQUurV7Gor%2BMmyOPdh25218wxSEnyTihc-kB%2BmvvA%40mail.gmail.com.


Re: [go-nuts] Interface arguments to generic functions

2021-01-19 Thread burak serdar
On Tue, Jan 19, 2021 at 10:37 PM Ian Lance Taylor  wrote:
>
> On Tue, Jan 19, 2021 at 8:02 PM burak serdar  wrote:
> >
> > On Tue, Jan 19, 2021 at 8:38 PM Ian Lance Taylor  wrote:
> > >
> > > On Tue, Jan 19, 2021 at 7:06 PM burak serdar  wrote:
> > > >
> > > > In the following program, it is valid to pass an interface to function 
> > > > P:
> > > >
> > > > func P[T fmt.Stringer](x T) {
> > > >  fmt.Println(x)
> > > > }
> > > >
> > > > func main() {
> > > >   var v fmt.Stringer
> > > >   P(v)
> > > > }
> > > >
> > > > However, there is no way for P to check if x is nil. This does not 
> > > > compile:
> > > >
> > > > func P[T fmt.Stringer](x T) {
> > > >  if x!=nil {
> > > > fmt.Println(x)
> > > > }
> > > > }
> > > >
> > > > Is it possible to write a generic function that can test if its
> > > > argument with a constraint is nil?
> > >
> > > For an interface type the value "nil" is the zero value of the type,
> > > so this is the general zero value issue mentioned at
> > > https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#the-zero-value
> > >
> > > You can write
> > >
> > > func P[T fmt.Stringer](x T) {
> > >  var zero T
> > >  if x!=zero {
> > > fmt.Println(x)
> > > }
> > > }
> > >
> >
> > But that breaks the generic function for non-interface types.
> >
> > type Str string
> >
> > func (s Str) String() string {return string(s)}
> >
> > func main() {
> >P(Str(""))
> > }
> >
> > Now the passed parameter is the zero value, but not nil.
>
>
> OK, but what are you really trying to check?  Why do you want to check
> that an interface is not the zero value while at the same time you
> don't care whether a string is the zero value?

As a generic-function writer, I do not know if the argument will be an
interface or a concrete type. I don't want to check if it is
zero-value, I want to check if it is nil, because I don't want it to
panic.

If I was to convert the following non-generic function to a generic one:

func f(x SomeIntf) {
  if x!=nil {
x.Something()
  }
 ...
}

the obvious way to do this is:

func f[T SomeIntf](x T) {
  ...
}

but I can no longer check if x is nil.

Treating nil-checks as a special case might work maybe?

>
> Ian

-- 
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/CAMV2RqqGVm%3DbiiAeKGHe5G%2By3WgZBSJGtbpA0ugZKngcW0Mgpw%40mail.gmail.com.


Re: [go-nuts] Interface arguments to generic functions

2021-01-19 Thread 'Dan Kortschak' via golang-nuts
On Tue, 2021-01-19 at 21:38 -0800, Ian Lance Taylor wrote:
> On Tue, Jan 19, 2021 at 8:41 PM Dan Kortschak 
> wrote:
> > 
> > Would that work for non-comparable types? Say the T has an
> > underlying
> > []int type, then the comparison is not against nil and you end up
> > with
> > a panic.
> 
> 
> Fair point.  But I'm not sure what the goal is here.  Perhaps if we
> can define that we can figure out how to make it work.
> 
> Ian

I can see value in it; we use the pattern sometimes of allocating a
slice for a nil destination and returning it or otherwise checking that
lengths match. That pattern would not be possible with this issue. The
use case would be single or double precision float slices. The same
thing happens with maps sometimes too.


-- 
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/0cb8abb6dc49bab6743e4817fcef8b1ac0fb1fcc.camel%40kortschak.io.


Re: [go-nuts] Interface arguments to generic functions

2021-01-19 Thread Ian Lance Taylor
On Tue, Jan 19, 2021 at 8:41 PM Dan Kortschak  wrote:
>
> On Tue, 2021-01-19 at 19:38 -0800, Ian Lance Taylor wrote:
> > On Tue, Jan 19, 2021 at 7:06 PM burak serdar 
> > wrote:
> > >
> > > In the following program, it is valid to pass an interface to
> > > function P:
> > >
> > > func P[T fmt.Stringer](x T) {
> > >  fmt.Println(x)
> > > }
> > >
> > > func main() {
> > >   var v fmt.Stringer
> > >   P(v)
> > > }
> > >
> > > However, there is no way for P to check if x is nil. This does not
> > > compile:
> > >
> > > func P[T fmt.Stringer](x T) {
> > >  if x!=nil {
> > > fmt.Println(x)
> > > }
> > > }
> > >
> > > Is it possible to write a generic function that can test if its
> > > argument with a constraint is nil?
> >
> > For an interface type the value "nil" is the zero value of the type,
> > so this is the general zero value issue mentioned at
> >
> https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#the-zero-value
> >
> > You can write
> >
> > func P[T fmt.Stringer](x T) {
> >  var zero T
> >  if x!=zero {
> > fmt.Println(x)
> > }
> > }
> >
> > Ian
>
> Would that work for non-comparable types? Say the T has an underlying
> []int type, then the comparison is not against nil and you end up with
> a panic.


Fair point.  But I'm not sure what the goal is here.  Perhaps if we
can define that we can figure out how to make it work.

Ian

-- 
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/CAOyqgcV_Je%2BxtetNnRBmiSxHYcTt%3Du4NvxNTHdgEMf1YQ7GQDQ%40mail.gmail.com.


Re: [go-nuts] Interface arguments to generic functions

2021-01-19 Thread Ian Lance Taylor
On Tue, Jan 19, 2021 at 8:02 PM burak serdar  wrote:
>
> On Tue, Jan 19, 2021 at 8:38 PM Ian Lance Taylor  wrote:
> >
> > On Tue, Jan 19, 2021 at 7:06 PM burak serdar  wrote:
> > >
> > > In the following program, it is valid to pass an interface to function P:
> > >
> > > func P[T fmt.Stringer](x T) {
> > >  fmt.Println(x)
> > > }
> > >
> > > func main() {
> > >   var v fmt.Stringer
> > >   P(v)
> > > }
> > >
> > > However, there is no way for P to check if x is nil. This does not 
> > > compile:
> > >
> > > func P[T fmt.Stringer](x T) {
> > >  if x!=nil {
> > > fmt.Println(x)
> > > }
> > > }
> > >
> > > Is it possible to write a generic function that can test if its
> > > argument with a constraint is nil?
> >
> > For an interface type the value "nil" is the zero value of the type,
> > so this is the general zero value issue mentioned at
> > https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#the-zero-value
> >
> > You can write
> >
> > func P[T fmt.Stringer](x T) {
> >  var zero T
> >  if x!=zero {
> > fmt.Println(x)
> > }
> > }
> >
>
> But that breaks the generic function for non-interface types.
>
> type Str string
>
> func (s Str) String() string {return string(s)}
>
> func main() {
>P(Str(""))
> }
>
> Now the passed parameter is the zero value, but not nil.


OK, but what are you really trying to check?  Why do you want to check
that an interface is not the zero value while at the same time you
don't care whether a string is the zero value?

Ian

-- 
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/CAOyqgcVrmR7HG2OwMhoNyV2BpfRNTK5LXihP-qRT%3DkGgtbpEhA%40mail.gmail.com.


Re: [go-nuts] Interface arguments to generic functions

2021-01-19 Thread burak serdar
On Tue, Jan 19, 2021 at 8:38 PM Ian Lance Taylor  wrote:
>
> On Tue, Jan 19, 2021 at 7:06 PM burak serdar  wrote:
> >
> > In the following program, it is valid to pass an interface to function P:
> >
> > func P[T fmt.Stringer](x T) {
> >  fmt.Println(x)
> > }
> >
> > func main() {
> >   var v fmt.Stringer
> >   P(v)
> > }
> >
> > However, there is no way for P to check if x is nil. This does not compile:
> >
> > func P[T fmt.Stringer](x T) {
> >  if x!=nil {
> > fmt.Println(x)
> > }
> > }
> >
> > Is it possible to write a generic function that can test if its
> > argument with a constraint is nil?
>
> For an interface type the value "nil" is the zero value of the type,
> so this is the general zero value issue mentioned at
> https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#the-zero-value
>
> You can write
>
> func P[T fmt.Stringer](x T) {
>  var zero T
>  if x!=zero {
> fmt.Println(x)
> }
> }
>

But that breaks the generic function for non-interface types.

type Str string

func (s Str) String() string {return string(s)}

func main() {
   P(Str(""))
}

Now the passed parameter is the zero value, but not nil.


> Ian

-- 
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/CAMV2Rqo61NNVSf_y54rq9-iCDcY%3DjDZk%2Bd3tzy4%3DMX3XL-VZug%40mail.gmail.com.


Re: [go-nuts] Interface arguments to generic functions

2021-01-19 Thread Ian Lance Taylor
On Tue, Jan 19, 2021 at 7:06 PM burak serdar  wrote:
>
> In the following program, it is valid to pass an interface to function P:
>
> func P[T fmt.Stringer](x T) {
>  fmt.Println(x)
> }
>
> func main() {
>   var v fmt.Stringer
>   P(v)
> }
>
> However, there is no way for P to check if x is nil. This does not compile:
>
> func P[T fmt.Stringer](x T) {
>  if x!=nil {
> fmt.Println(x)
> }
> }
>
> Is it possible to write a generic function that can test if its
> argument with a constraint is nil?

For an interface type the value "nil" is the zero value of the type,
so this is the general zero value issue mentioned at
https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#the-zero-value

You can write

func P[T fmt.Stringer](x T) {
 var zero T
 if x!=zero {
fmt.Println(x)
}
}

Ian

-- 
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/CAOyqgcUyu6ik0J7zURT5xLLm_s7TmEszXJUkuKvDKCrbeeeRaA%40mail.gmail.com.


[go-nuts] Interface arguments to generic functions

2021-01-19 Thread burak serdar
In the following program, it is valid to pass an interface to function P:

func P[T fmt.Stringer](x T) {
 fmt.Println(x)
}

func main() {
  var v fmt.Stringer
  P(v)
}

However, there is no way for P to check if x is nil. This does not compile:

func P[T fmt.Stringer](x T) {
 if x!=nil {
fmt.Println(x)
}
}

Is it possible to write a generic function that can test if its
argument with a constraint is nil?

-- 
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/CAMV2Rqp3OV%2BOHTYJL3zkBbssktOSaRQDqz7fzSoeLU002YTZ1Q%40mail.gmail.com.