4 ms·
This is a lot of complication for in what most OOP languages with interfaces would simply be something like: interface Timestamped { timestamp: UTCTi
by spankalee 2y ago
This is a lot of complication for in what most OOP languages with interfaces would simply be something like:
interface Timestamped {
timestamp: UTCTime;
}
interface Msg {
sender: PlayerId;
}
class Quote implements Timestamped, Msg {
timestamp: UTCTime;
sender: PlayerId;
}
Why is this so hard in Haskell? It doesn't have interface polymorphism?
- keybored 2y agoI think TFA is talking about nesting fields in whatever order and also being able to extract the fields without ceremony like `get(get(get...))`—just allow `get(...)`. If you wanted an example more close to the submission you would have a multi-field class inside that class. Then you would perhaps solve the nested field access boilerplate by manually implementing it in the interface methods.
- valcron1000 2y agoIt's not hard at all and it's actually what the second approach listed by the author shows: class HasRecipient a where get_receiver :: a -> PlayerId which adjusted to your example would be class Timestamped a where timestamp :: a -> UTCTime The problem with this approach is that you'll have duplicated data on all instances. In your example, `Quote` has the fields `timestamp` and `sender` in order to satisfy with `Timestamped` and `Msg`. If you had several classes like `Quote` and interfaces like `Timestamped` then you would end up with a lot of duplicated code.
- eddd-ddde 2y agoDoesn't PHP solve this with traits? IIRC you can have traits automatically implement this sort of behavior with a centralized implementation.
- mrkeen 2y ago> Why is this so hard in Haskell? The challenge is composition. Adding Timestamped to something in a static, type-checked way, but not modifying the source code of that something. > It doesn't have interface polymorphism? This is besides the point, but its interface polymorphism is static, not dynamic. If you have a List which is IMappable, and you do some IMappable operations on it, in most OOP languages you get back an IMappable, but in Haskell you get back List.