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