To me, the first confusing part is that in some places you need to write "T 
any" to accept any type (unconstrained), and in other places you can write 
bare "T"

The second confusing part is that you can't instantiate a parameterized 
type in the way you might expect, so in this case Result[bool] isn't a 
concrete type with the parameter set to "bool", but is really Result[T].  
You have to stamp out a new type:
https://gotipplay.golang.org/p/iYHguVsEEbZ   # gives the error the OP 
expected (perhaps)
https://gotipplay.golang.org/p/1fzwaoI7fyj   # fixed

The other potential source of confusion is that a type *parameter* can 
shadow the name of an existing defined type: so "bool" is still some type, 
but not the type it conventionally is. However, you can already do similar 
confusing things, like have a variable which shadows the name of a type:
https://go.dev/play/p/z6E5fm8x528

Or even directly shadow a built-in type:
https://go.dev/play/p/i7IB_nQdcFj

On Thursday, 16 December 2021 at 09:39:44 UTC axel.wa...@googlemail.com 
wrote:

> It doesn't do "complete nonsense". It's equivalent to writing `func (r 
> Result[T])`, it just uses a different identifier for the type-parameter.
> I agree that this looks confusing, which is why you shouldn't do it. 
> Perhaps it would be better to require the type-parameter names in method 
> declarations to have the same names as in the type declaration. But note 
> that even that wouldn't prevent you from doing
>
> type Result[bool any] struct {}
>
> func (r Result[bool]) Method() { /* … */ }
>
> which still looks exactly as confusing.
>
> On Thu, Dec 16, 2021 at 10:24 AM Aliaksandr Mianzhynski <
> amenz...@gmail.com> wrote:
>
>> I'm a bit confused about ability to change struct constraints in methods 
>> that it looks like a method definition for a specific type, but in reality 
>> it does complete nonsense:
>>
>> Here's an example: https://gotipplay.golang.org/p/zFLJMdp5mbO
>>
>> -- 
>> 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/ad9317b8-aa69-4077-b24d-f6639eaf55e7n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/ad9317b8-aa69-4077-b24d-f6639eaf55e7n%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/c6956cf3-a782-423f-b4bc-3421f4f26551n%40googlegroups.com.

Reply via email to