3 ms·
Imagine I have a JSONfiable protocol and I have a generic function to traverse a structure that uses the protocol to make a JSON document. It would be nice if t
by pdhborges 15y ago
Imagine I have a JSONfiable protocol and I have a generic function to traverse a structure that uses the protocol to make a JSON document. It would be nice if the caller had a way to change the format.
- puredanger 15y agoYou can change the implementation of the protocol, but then it's changed (in that it replaces the prior version). When you see something of type X, the protocol says what to do when you encounter a function on X. If you want more than one kind of thing to happen, then you shouldn't use a protocol. For instance, it would be perfectly feasible to use a multimethod that dispatches on both type and output format. Or a protocol that dispatches on type, then dispatches a different function on format.