[go-nuts] Re: Extending objects / methods

2016-12-02 Thread Slawomir Pryczek
Thanks for the post, yes basically i don't want to inherit old methods... 
the member approach would work for me but im thinking if that won't be 
slow, probably will do some tests... basically im trying to find some way 
of doing this without need of additional struct with just one member...

type xadvanced *x
defining type xadvanced (with 0 methods), adding some methods to xadvanced, 
which will access methods of "parent" type x ... by doing something like 
"pointer conversion" in C, but I didn't found a way of doing this ... so 
that's maybe not possible



W dniu piątek, 2 grudnia 2016 16:39:31 UTC+1 użytkownik parais...@gmail.com 
napisał:
>
> The declared type does not inherit any methods 
>>  bound to the existing 
>> type, but the method set  of an 
>> interface type or of elements of a composite type remains unchanged:
>
>
> https://golang.org/ref/spec#Type_declarations
>
> You need to use another strategy like struct embedding which *may *or *may 
> not* work depending to your use case
>
> A field declared with a type but no explicit field name is an *anonymous 
>> field*, also called an *embedded* field or an embedding of the type in 
>> the struct. An embedded type must be specified as a type name T or as a 
>> pointer to a non-interface type name *T, and T itself may not be a 
>> pointer type. The unqualified type name acts as the field name.
>
>
> type xadvanced struct {
> *x
> }
> func(x *xadvanced)increment(){
>x.y ++
>fmt.Println(x.y)
> }
>
> I'd advise you do read the spec at least once, it's short and concise .
>
>
> Le vendredi 2 décembre 2016 16:25:50 UTC+1, Slawomir Pryczek a écrit :
>>
>> Hi Guys,
>>
>> i want to make something like this
>>
>> type si struct {
>>
>> s *sync_task.Sync_task
>>
>> }
>>
>>
>> func (this *si) Process() {
>>
>>
>>... some code here ...
>>
>>this.Process();
>>
>> }
>>
>>
>>
>> Basically i want to extend object in another package... and this works. 
>> Now i'd just want to extend it without creating "real" struct, code
>>
>> type x struct {
>> y int
>> }
>> type xadvanced *x
>>
>> func (this *x) increment() {
>> this.y++
>> fmt.Println(this.y)
>> }
>>
>> test2 := xadvanced()
>> test2.increment() ---> ERROR
>>
>> https://play.golang.org/
>>
>> What im doing wrong and how to access methods of x object when having 
>> pointer to xadvanced...
>>
>

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


[go-nuts] Re: Extending objects / methods

2016-12-02 Thread paraiso . marc

>
> The declared type does not inherit any methods 
>  bound to the existing 
> type, but the method set  of an 
> interface type or of elements of a composite type remains unchanged:


https://golang.org/ref/spec#Type_declarations

You need to use another strategy like struct embedding which *may *or *may 
not* work depending to your use case

A field declared with a type but no explicit field name is an *anonymous 
> field*, also called an *embedded* field or an embedding of the type in 
> the struct. An embedded type must be specified as a type name T or as a 
> pointer to a non-interface type name *T, and T itself may not be a 
> pointer type. The unqualified type name acts as the field name.


type xadvanced struct {
*x
}
func(x *xadvanced)increment(){
   x.y ++
   fmt.Println(x.y)
}

I'd advise you do read the spec at least once, it's short and concise .


Le vendredi 2 décembre 2016 16:25:50 UTC+1, Slawomir Pryczek a écrit :
>
> Hi Guys,
>
> i want to make something like this
>
> type si struct {
>
> s *sync_task.Sync_task
>
> }
>
>
> func (this *si) Process() {
>
>
>... some code here ...
>
>this.Process();
>
> }
>
>
>
> Basically i want to extend object in another package... and this works. 
> Now i'd just want to extend it without creating "real" struct, code
>
> type x struct {
> y int
> }
> type xadvanced *x
>
> func (this *x) increment() {
> this.y++
> fmt.Println(this.y)
> }
>
> test2 := xadvanced()
> test2.increment() ---> ERROR
>
> https://play.golang.org/
>
> What im doing wrong and how to access methods of x object when having 
> pointer to xadvanced...
>

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