Re: [go-nuts] Thinking OO virtual function in Go

2016-11-24 Thread Tong Sun
Yeah, me too, preferring the first version, which I've archived as a masterpiece at https://github.com/suntong/lang/blob/master/lang/Go/src/oo/Polymorphism-AnimalVF3.go with the following notes: The best way to implement *virtual function* so far, that satisfies the following challenges: - Consid

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-24 Thread Tong Sun
Oh, I like that. This is much better than my "double-passing" version, and so far the best. Thanks a lot! On Thu, Nov 24, 2016 at 9:21 PM, xingtao zhao wrote: > Hi Tong, > > Another implementation is https://play.golang.org/p/3_PDTCcJTi. > You could merge the interface Animal and ExtraFactsor to

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-24 Thread xingtao zhao
Or this implementation: https://play.golang.org/p/5GqspHDJnF But I prefer the previous version: https://play.golang.org/p/3_PDTCcJTi. On Thursday, November 24, 2016 at 6:21:24 PM UTC-8, xingtao zhao wrote: > > Hi Tong, > > Another implementation is https://play.golang.org/p/3_PDTCcJTi. > You could

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-24 Thread xingtao zhao
Hi Tong, Another implementation is https://play.golang.org/p/3_PDTCcJTi. You could merge the interface Animal and ExtraFactsor together if this extra behavior is not needed. It is listed there just for demonstration. On Thursday, November 24, 2016 at 12:14:37 AM UTC-8, Nick Patavalis wrote: > >

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-24 Thread Nick Patavalis
On Thu, Nov 24, 2016 at 10:00 AM, Haddock wrote: > > This is merely resorting to a shared function. This does not really > cover overwriting in the OO sense. The article I mentioned shows how > to do this: > http://objectscape.blogspot.de/2013/09/inner-pattern-to-mimic-method.html Not really! Ton

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-24 Thread Haddock
> I think I finally found the easiest way to make virtual function works, > based on Nick Patavalis code and Tahir Hashmi's idea: > > > func (a Animal) Output(s Speaker) { > // Complicated stuff that must not be re-implemented > fmt.Print("I am a ", s.IsA(), > ". My name is ", a.Name, > ", aged

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Nick Patavalis
​ As I said ​ ​ before, Go's flavor of OO is different from that of other languages. Don't try to map concepts one-to-one, or you will end up with very contrived solutions. Always look at the big picture (why am I doing this?) and don't get side-tracked by synthetic examples. Most important is def

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Tong Sun
On Wed, Nov 23, 2016 at 3:49 PM, Tong Sun wrote: > > On Wed, Nov 23, 2016 at 12:09 PM, Nick Patavalis wrote: > >> For this specific example, something like this: >> https://play.golang.org/p/FsorWRaLKk >> > > Thanks a lot Nick! > > I've simplified it a bit. Now it is: > > func (d Dog) Output() { >

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Tong Sun
On Wed, Nov 23, 2016 at 12:09 PM, Nick Patavalis wrote: > For this specific example, something like this: https://play.golang.org/p/ > FsorWRaLKk > Thanks a lot Nick! I've simplified it a bit. Now it is: func (d Dog) Output() { // Presumably complicated stuff, not re-implemented d.Animal.Output

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Tong Sun
Thanks Chad. This is beyond my understanding for the moment, but I'm sure it'd be handy someday... Thanks. On Wed, Nov 23, 2016 at 1:17 PM, wrote: > Sorry, didn't read the whole thread, but here's how I tackled inheritance > in my cross compiler: https://play.golang.org/p/UDp7nSLyl2. Granted, I

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread chad . retz
Sorry, didn't read the whole thread, but here's how I tackled inheritance in my cross compiler: https://play.golang.org/p/UDp7nSLyl2. Granted, I am unsure about the unsafe thing, but the concept of a "method dispatcher" is likely what you need. On Wednesday, November 23, 2016 at 11:09:03 AM UTC

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Nick Patavalis
On Wednesday, November 23, 2016 at 5:17:11 PM UTC+2, Tong Sun wrote: > > Can you make it work on play.golang.org, from this code > https://play.golang.org/p/QjCtD9rGpa, according to your plan? > For this specific example, something like this: https://play.golang.org/p/FsorWRaLKk /npat -- You

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Nick Patavalis
Yes... so every specific animal type implements it's own Output() method, which does the trivial IsA() part, and calls Animal's Output() for the common complicated parts... On Nov 23, 2016 16:35, "Tong Sun" wrote: > Have you noticed the IsA() func call there? > > On Wed, Nov 23, 2016 at 4:05 AM,

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Tong Sun
Oh, I think you might not have notice this request from OP: Please think of the "func Output()" as a very complicated function that I > only want to define *once *at the base level, not to duplicate into each > sub classes. On Wed, Nov 23, 2016 at 10:16 AM, Tong Sun wrote: > Can you make it w

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Tong Sun
Can you make it work on play.golang.org, from this code https://play.golang.org/p/QjCtD9rGpa, according to your plan? On Wed, Nov 23, 2016 at 10:14 AM, Nick Patavalis wrote: > Yes... so every specific animal type implements it's own Output() method, > which does the trivial IsA() part, and calls

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Tong Sun
Have you noticed the IsA() func call there? On Wed, Nov 23, 2016 at 4:05 AM, Nick Patavalis wrote: > Hi, > > In your *second* example, making Output() a method of Animal will work, > since it uses only the members (fields) of Animal, and not the fields of > specific animals (or any behavior that

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Tong Sun
Further on that, apart from the getters, is it possible to define a left-value function, so that I can use it to update the member values? E.g., from https://play.golang.org/p/jlgIFjI268, func (a *Animal) TheAge() int { return a.Age } or func (a *Animal) TheAge() *int { return &a.Age } *d.TheAge(

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-23 Thread Nick Patavalis
Hi, In your *second* example, making Output() a method of Animal will work, since it uses only the members (fields) of Animal, and not the fields of specific animals (or any behavior that varies between animals). That's why I'm insisting on *real* and *specific* examples, not synthetic ones. /

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
Thanks a lot for your explicit example. Much more helpful to me than merely saying define getters. Much appreciate it! On Tue, Nov 22, 2016 at 6:27 PM, wrote: > interfaces only work on methods. > > https://play.golang.org/p/o6Ot4IdJZ1 > > Name, Age , ... are not methods they are fields.You need

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
No Nick, making Output() a member method won't work. See my OP and Jesse's answer. I.e., I have to change it from a member function to a pure function. On Tue, Nov 22, 2016 at 6:25 PM, Nick Patavalis wrote: > Hi, > > There is no direct mapping of what you can do with virtual functions in > other

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
On Tue, Nov 22, 2016 at 6:23 PM, Jesse McNelis wrote: > On 23 Nov. 2016 9:03 am, "Tong Sun" wrote: > > > > So, once again, thinking in OO, I'll define all of the common variables > in base class, and common functionalities in virtual functions. How to make > that idea work in Go? > > > > For the a

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread paraiso . marc
interfaces only work on methods. https://play.golang.org/p/o6Ot4IdJZ1 Name, Age , ... are not methods they are fields.You need to make them part of Speaker interface by using methods instead of fields. Le mardi 22 novembre 2016 23:03:54 UTC+1, Tong Sun a écrit : > > > > On Tue, Nov 22, 2016 at

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Nick Patavalis
Hi, There is no direct mapping of what you can do with virtual functions in other OO languages, and Go. There are different compromises you have to make; because of this, synthetic examples will probably not help much. That being said, in the case of your last example I would make Output() a

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Jesse McNelis
On 23 Nov. 2016 9:03 am, "Tong Sun" wrote: > > So, once again, thinking in OO, I'll define all of the common variables in base class, and common functionalities in virtual functions. How to make that idea work in Go? > > For the above specific code, how to easily make "func Output" works? > You c

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
On Tue, Nov 22, 2016 at 4:29 PM, Jesse McNelis wrote: > On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun wrote: > > Hi, > > > > How to architect the OO's virtual function in Go? > > > > Please take a look at this (not working) Go program > > https://play.golang.org/p/qrBX6ScABp > > > > Please think of th

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Tong Sun
On Tue, Nov 22, 2016 at 4:29 PM, Seb Binet wrote: > > > On Tue, Nov 22, 2016 at 10:16 PM, Tong Sun wrote: > >> Hi, >> >> How to architect the OO's virtual function in Go? >> >> Please take a look at this (not working) Go program >> https://play.golang.org/p/qrBX6ScABp >> >> Please think of the "fu

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Seb Binet
On Tue, Nov 22, 2016 at 10:16 PM, Tong Sun wrote: > Hi, > > How to architect the OO's virtual function in Go? > > Please take a look at this (not working) Go program > https://play.golang.org/p/qrBX6ScABp > > Please think of the "func Output()" as a very complicated function that I > only want to

Re: [go-nuts] Thinking OO virtual function in Go

2016-11-22 Thread Jesse McNelis
On Wed, Nov 23, 2016 at 8:16 AM, Tong Sun wrote: > Hi, > > How to architect the OO's virtual function in Go? > > Please take a look at this (not working) Go program > https://play.golang.org/p/qrBX6ScABp > > Please think of the "func Output()" as a very complicated function that I > only want to d