3 ms·
Go is often thought of as a successor of C. C doesn't have methods, only global functions. From my perspective, Go added methods primarily so that you can use t
by EdSchouten 1mo ago
Go is often thought of as a successor of C. C doesn't have methods, only global functions. From my perspective, Go added methods primarily so that you can use them in combination with interfaces. Given that interfaces don't support generic methods, I'm personally not convinced that this feature was worth adding.
- munificent 1mo ago> From my perspective, Go added methods primarily so that you can use them in combination with interfaces. They also give you a limited form of overloading. Without methods or overloading, you end up in the situation that C and Scheme are in where every operation on a data structure has to redundantly have the data structure in its name like: list_clear(my_list); queue_clear(my_queue); map_clear(my_map);
- kune 1mo agoIn Go you would methods for that: my_list.Clear(), my_queue.Clear() and my_map.Clear(). Now you can define a Clearer interface, which has only the Clear method. That allows you to write a function clearAndLog(item Clearer) and it will work with the list, queue and map.
- munificent 1mo agoYes, that's my point. Go doesn't have overloading by parameter list signature. But you can have methods with the same name defined on different types, so there is a sort of overloading or namespacing based on the receiver type. Methods give you that.
- wasmperson 1mo ago> Without methods or overloading, you end up in the situation that C and Scheme are in Well in C at least we now have this: #define clear(s) _Generic((s) \ ,struct list: list_clear \ ,struct queue: queue_clear \ ,struct map: map_clear \ )(s) clear(my_map); clear(my_list); clear(my_queue); ...although it turns out the other nice thing about methods is automatic namespacing.
- deleted 1mo ago[deleted]
- so-cal-schemer 1mo agoI just want to leave this here: SICP: 2.5 Systems with Generic Operations https://sarabander.github.io/sicp/html/2_002e5.xhtml https://sarabander.github.io/sicp/html/2_002e5.xhtml
- etse 1mo agoWhat was the reason for interfaces having to work at runtime?
- duskwuff 1mo agoThe methods in an interface can be implemented by many different types, and it's often hard or impossible to determine which of those types will be passed in to a function. For example, the io.Reader interface is implemented by many different stream-like types, and functions which accept io.Reader arguments generally can't make assumptions about which of those types they'll get.
- masklinn 1mo agoThat dynamic dispatch and type erasure are literally the purpose of interfaces?
- pjmlp 1mo agoWell, C has a kind of pseudo generics since C11. And everyone gets to invent their own vtable implementation since the 1980's.