Re: [go-nuts] Go 1.21 new builtin function clear()

2023-07-06 Thread 'Axel Wagner' via golang-nuts
On Thu 6. Jul 2023 at 09:41, Henry wrote: > 'make' allocates the required memory. Does it? What about channels and maps? 'len' returns the length. What’s the “length” of a channel? What’s the “length” of a map? 'cap' returns the capacity. For maps? These questions are rhetorical, for

Re: [go-nuts] Go 1.21 new builtin function clear()

2023-07-06 Thread Brian Candler
In principle I agree with the sentiment, in the sense this is what you'd expect from other languages like Python. However, slices are fundamentally different to maps in Go. Map values contain a pointer to a mutable data structure (that's why you can't insert into a zero map - you have to

Re: [go-nuts] Go 1.21 new builtin function clear()

2023-07-06 Thread Henry
'make' allocates the required memory. 'len' returns the length. 'cap' returns the capacity. The underlying implementation may be different, but the concept is the same. There is no issue with those. It is common for a collection to have methods such as 'Add', 'Delete', and 'Clear'. The common

Re: [go-nuts] Go 1.21 new builtin function clear()

2023-07-06 Thread 'Axel Wagner' via golang-nuts
Oh and FWIW: You are right (in my opinion) that the different things `clear` does are, well, different. But note that clear is not the only builtin for which that is the case. `make`, `len` and `cap` all do different things (to varying degrees) on maps, slices and channels. That's not necessarily

Re: [go-nuts] Go 1.21 new builtin function clear()

2023-07-06 Thread 'Axel Wagner' via golang-nuts
On Thu, Jul 6, 2023 at 7:49 AM Henry wrote: > So, if I get this right, clear on map will result in map length equals to > zero, but clear on slice is only a value-zeroing operation and the slice > length remains unchanged? That understanding is correct. > They seem like two different

Re: [go-nuts] Go 1.21 new builtin function clear()

2023-07-05 Thread Henry
So, if I get this right, clear on map will result in map length equals to zero, but clear on slice is only a value-zeroing operation and the slice length remains unchanged? They seem like two different operations to me. I don't think that built-in clear function is necessary. It doesn't seem

Re: [go-nuts] Go 1.21 new builtin function clear()

2023-07-05 Thread Tharaneedharan Vilwanathan
Hi Axel, Okay, that helps! Thanks for the details. Regards dharani On Wed, Jul 5, 2023 at 1:38 AM Axel Wagner wrote: > Hi, > > this has come up on the issue as well. Robert Griesemer provided an > explanation > : > > If the

Re: [go-nuts] Go 1.21 new builtin function clear()

2023-07-05 Thread 'Axel Wagner' via golang-nuts
Hi, this has come up on the issue as well. Robert Griesemer provided an explanation : If the argument type (the type of the argument provided to clear) is a type > parameter (is of type parameter type), all types in its type set

[go-nuts] Go 1.21 new builtin function clear()

2023-07-05 Thread Tharaneedharan Vilwanathan
Hi All, Go 1.21 introduces a new clear() builtin function. I see this text in https://tip.golang.org/ref/spec#Clear: clear(t) type parameter see below If the argument type is a type parameter , all types in its type set must be maps