Re: [go-nuts] Polymorphism in GO

727 views
Skip to first unread message

LRN

unread,
Jul 11, 2012, 3:54:55 PM7/11/12
to golan...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 11.07.2012 21:53, amiw...@gmail.com wrote:
> Hay Guys!
>
> I am looking for information about the polymorphism in GO. Can
> anyone help me find an informative topics? Explanations are great
> too!
According to [1]:

Ad-hoc polymorphism: Supported with interfaces. You call a method with
the same name on any number of objects that implement a particular
interface, and they will behave differently, depending on the
implementation.

Parametric polymorphism: No generics. But if you write a function that
works with interfaces (well, object implementing interfaces), not
types, then you can have parametric polymorphism as well (a single
function implementation that works correctly, and with the same
algorithm, on any type that implements the right interface correctly).

Subtype polymorphism: Not applicable (Go doesn't have subtyping).
Although if you embed a struct A implementing interface X into a
struct B, struct B will implement interface X, and can be used instead
of struct A in places where struct A is expected. So, kind of yes.

[1] http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJP/doMAAoJEOs4Jb6SI2Cw+qcH/3kruQ/BKmiMf3oGaOMGkGTQ
Lzqs2JKhlI8vP9ImNExslRuV3K7mgZof1eRci1U9qigDoWNNaVm7o3r4RzOKjYNo
LOZWQVGmTFXxgj1j7xOfoHGPDm/ZkODph6yJoV2zRimm5PbFzi3GLcmBaKP76kW3
HsY9QE7XUSOgxmhStE+myF3nfbmcyaY8oodswl/Hu3tSano02/0LXgutzYFIBjc7
aJdsUtGcHtXxluddFIwXCRdqg89E5O9X82b3hnwptFtAMucDWBmQj/8s9kLEcBLH
Yt8ZgLt89ZEl5cdaZOBkIyYLKN9OtX5mIZyb12S19ldcATYXB4Eu6peeY7gQ21w=
=mveg
-----END PGP SIGNATURE-----

Bob Hutchison

unread,
Jul 11, 2012, 4:55:56 PM7/11/12
to golan...@googlegroups.com

On 2012-07-11, at 3:54 PM, LRN wrote:

> Subtype polymorphism: Not applicable (Go doesn't have subtyping).
> Although if you embed a struct A implementing interface X into a
> struct B, struct B will implement interface X, and can be used instead
> of struct A in places where struct A is expected. So, kind of yes.

I think saying "kind of" here is not doing anyone any favours. I really do not think that this behaviour would be expected from someone familiar with OO. In fact, I think they'd find it to be pretty surprising. Moreover, I'll bet many would read that paragraph you wrote and not see what you wrote, they'd see what they expected to see :-) Take a look at http://play.golang.org/p/Sji10MCx-h Many people familiar with OO (and unfamiliar with Go) will be quite surprised at the output of that program.

Cheers,
Bob

Robert Johnstone

unread,
Jul 11, 2012, 5:17:40 PM7/11/12
to golan...@googlegroups.com
Except for some built-in function, the only type of polymorphism that Go supports is interfaces.  If you want polymorphism, then that is where you need to start and finish. 

1) This trips some people up, but you will need to rely on runtime polymorphism exclusively.  Go does not have generics.  Look at the package "sort" for an idea of how this works in practice.
2) For many applications, interfaces behave similarly to virtual functions, but they are not identical.  See the example program supplied by hutch.

Sathish VJ

unread,
Jul 12, 2012, 9:08:25 AM7/12/12
to golan...@googlegroups.com
I've tried some samples in this tutorial: http://golangtutorials.blogspot.fr/2011/06/polymorphism-in-go.html.


On Wednesday, 11 July 2012 19:53:11 UTC+2, amiw...@gmail.com wrote:
Hay Guys!

I am looking for information about the polymorphism in GO.
Can anyone help me find an informative topics? 
Explanations are great too!


Thanks!
Ami

si guy

unread,
Jul 12, 2012, 2:54:55 PM7/12/12
to golan...@googlegroups.com
You can use closures to do it, I've met with limited success doing this though.
Here's an example:
http://lispy.wordpress.com/2008/09/25/roll-your-own-oo-system-with-closures-and-a-dispatch-table/
Reply all
Reply to author
Forward
0 new messages