4 ms·
Is there any advantage to using OO in a language that doesn't have classes or inheritance? Does declaring a method on a type give you something that declaring a
by Touche 13y ago
Is there any advantage to using OO in a language that doesn't have classes or inheritance? Does declaring a method on a type give you something that declaring a function that takes the type as an argument doesn't?
- riobard 13y agoOO does not have to use classes and inheritance. OO is about encapsulation of data and behaviors. Declaring a function on a type lets you specify the behaviors of the type. In particular, in languages like Go with interfaces, it allows you to have a contract of behaviors independent of implementations. You cannot do that with plain functions.
- masklinn 13y ago> Is there any advantage to using OO in a language that doesn't have classes or inheritance? Like Self or javascript?
- adamlett 13y agoJavaScript and Self don't have classes, but they do have inheritance.
- masklinn 13y agoSelf has delegation. Conflating delegation and inheritance is no better than conflating embedding and inheritance.
- munificent 13y agoDelegation seems pretty similar to inheritance to me. What am I missing?
- masklinn 13y agoIt doesn't set up is-a relationships and can be used for significantly more than differential inheritance (IIRC Self uses it for scoping).
- laumars 13y agoThe lack of inheritance and creation methods are a bit annoying at times (particularly the latter), but there's still benefits of adopting an OOP approach in Go. For example, say you're writing a CMS and want to make strings like the article title to be used in the URL (for readable URLs and SEO), displayed in HTML (eg inside heading tags) and also potentially used for string comparisons, you'd need to have a function to URL / HTML encode the string and you'd need to remember to do it each time you outputted the string. This can make it very easy to introduce vulnerabilities where you forget to HTML encode the string. With methods, you can force the developer to state which format to output the string as: type DisplayText struct { Value string } func (dt DisplayText) HTMLEscaped() string { return html.EscapeString(dt.Value) } func (dt DisplayText) URLEscaped() string { return url.QueryEscape(dt.Value) } var article_title DisplayText So now when ever you call article_title, you have to specify the string encoding. Which is not only more secure (eliminates the risk of forgetting to encode your string), but also more readable: fmt.Println(article_title.HTMLEscaped) vs fmt.Println(html.EscapeString(article_title))
- qznc 13y agoOOP is about polymorphism. Classes and inheritance are not necessary, just like encapsulation or private data. In long: http://beza1e1.tuxen.de/articles/oop.html http://beza1e1.tuxen.de/articles/oop.html The question is not where the type syntactically is. The question is, if the dispatching between functions/methods happens dynamically or statically.
- pjmlp 13y agoThere are many ways to implement OO. Self, JavaScript, CLOS, Go, BETA, Clojure Protocols, Type Classes, COM and probably quite a few others. What most mainstream developers know, is not the only way.
- patrickg 13y ago+1 for mentioning BETA. Any actual user of that language?
- pjmlp 13y agoI got to learn about it when I attended ECOOP'99. Never saw it outside the conference.
- patrickg 13y agoBETA was the beginner's language at the University of Dortmund (Germany) back in 1996/7/8. Never saw it outside of these buildings.
- draegtun 13y agoBETA did come up in this HN post from last year: The impoliteness of overriding methods - https://news.ycombinator.com/item?id=4943538 https://news.ycombinator.com/item?id=4943538
- 13y ago
- luikore 13y agoToo many people were trained to think in the OO style, especially class-based OO, no matter if it fits the problem or not. Any language with prototype but not classes will end up with hand-crafted classes. Any language without neither will end up with hand-crafted prototypes.
- ori_b 13y agoYes. It gives you Liskov substitution.
- dragonwriter 13y ago> Is there any advantage to using OO in a language that doesn't have classes or inheritance? Yes. Really, interfaces are all you need for OO; class are just types-that-also-define-interfaces, and inheritance is just a shortcut for interface implementation. While I think Go's implicit interface implementation is pretty much 180-degrees off the best way to do OO w/o classes and inheritance (I'd prefer explicit interface implementation, which eliminates interface collision), I do think clasess and inheritance get in the way more than they help.