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

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

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

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. > >

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

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

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

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

[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 {