Hacker News new | past | comments | ask | show | jobs | submit login

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?



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


OOP is about polymorphism. Classes and inheritance are not necessary, just like encapsulation or private data. In long: 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.


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


+1 for mentioning BETA. Any actual user of that language?


BETA did come up in this HN post from last year: The impoliteness of overriding methods - https://news.ycombinator.com/item?id=4943538


I got to learn about it when I attended ECOOP'99.

Never saw it outside the conference.


BETA was the beginner's language at the University of Dortmund (Germany) back in 1996/7/8. Never saw it outside of these buildings.


Really?! Living in Düsseldorf, small world HN. :)


Yes, I still have my book "Object Oriented Programming in the BETA Programming Language" by Madsen/Møller-Pedersen/Nygaard with a price tag of 80 DM in 1997 (> 40 EUR now) (I now live in Berlin)


No, but the Racket OO system is based on BETA's implementation of OO, and Racket has some actual users. There's definitely some interesting ideas there.

For example, `inner` being the opposite of super (e.g. call the subclasses implementation of this method). Some others, but I'm not sure I remember them (Augmentation?).


The 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))


The last line was probably intended as

  fmt.Println(html.EscapeString(article_title))


You're right, it was. I'll correct that now.


> Is there any advantage to using OO in a language that doesn't have classes or inheritance?

Like Self or javascript?


JavaScript and Self don't have classes, but they do have inheritance.


Self has delegation. Conflating delegation and inheritance is no better than conflating embedding and inheritance.


Delegation seems pretty similar to inheritance to me. What am I missing?


It doesn't set up is-a relationships and can be used for significantly more than differential inheritance (IIRC Self uses it for scoping).


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


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


Yes. It gives you Liskov substitution.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: