by munificent · 3mo ago · 0 comments
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);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.
SICP: 2.5 Systems with Generic Operations
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.