On Sunday, February 24, 2019 at 9:36:07 PM UTC, rog wrote:
>
> Foo is declared as a new type, and no methods are declared on that that 
> type. If you had declared:
>
>     type Foo = *Foo
>
> it would have been a different matter.
>

true. however, i don't want a type alias because that would make it 
possible to copy f2 by dereferencing the pointer (f3 = *f2).

the reason i don't want that is because in my real use case foo contains a 
pointer to a CGo struct, which has its lifetime managed by a finalizer.
i don't want copies of that pointer made, so i thought that i can force 
pointer-only usage of foo (via Foo) and that'd be that.

it's also not possible to declare methods on pointer types (for a reason 
explained here 
<https://groups.google.com/d/msg/golang-nuts/qf76N-uDcHA/DTCDNgaF_p4J>, 
which makes sense), so `func (f Foo) GetBar() { ... }` is a no-go.

it still seems to me that Foo.GetBar should resolve to (*foo).GetBar, since 
Foo -> *foo is unambiguous. why not?
 

>
> On Sun, 24 Feb 2019, 10:30 pm , <roj...@gmail.com <javascript:>> wrote:
>
>> Why can't Go resolve methods declared on the pointer receiver of the 
>> destination type?
>>
>> consider:
>>
>> type foo struct{
>> bar int
>> }
>>
>> type Foo *foo
>>
>> func (f *foo) GetBar() int {
>> return f.bar
>> }
>>
>> func NewFoo(bar int) Foo {
>> return &foo{bar: bar}
>> }
>>
>> func main() {
>> f1 := &foo{bar: 123}
>> fmt.Printf("%d\n", f1.GetBar())
>> f2 := NewFoo(456)
>> fmt.Printf("%d\n", f2.GetBar())
>> }
>>
>>
>> (playground link: https://play.golang.org/p/v0f9pYaTJAa )
>>
>> it fails to compile with "prog.go:25:23: f2.GetBar undefined (type Foo 
>> has no field or method GetBar)" but it's not clear to me, why Foo doesn't 
>> get pointer methods from *foo.
>>
>> -- 
>> 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 <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to