3 ms·
The way I often explain it to a C++ developer is by asking this question: "what if the semi-colon was merely an operator, that you can overload?" Then they sor
by toolslive 9y ago
The way I often explain it to a C++ developer is by asking this question:
"what if the semi-colon was merely an operator, that you can overload?"
Then they sort of start to see the power.
- chopin 9y agoHow would you overload that for getting something monadic-like? Is there a simple example (C++ is obviously not my native tongue)?
- beojan 9y agoIt would be a sort of bind operator, I think. It wouldn't work exactly because you can't distinguish between `>>=` and `>>`.
- dllthomas 9y agoIt would work, it's just incomplete. `;` is `>>`, `>>=` would be involved in declaration of local variables.
- toolslive 9y agowell the (;) is the `bind. The problem with the question is that it only works on a conceptual level as C++ does not have type system support for statements. (ie, in {a;b} what's the type of a ?) But, it allows them to see how you could build fiber/promises/lightweight_threads support using this concept, which is most of the times what they are interested in anyway.
- dllthomas 9y agoIt could pass statements as functions. Imagine a syntactic transformation from `a; b` to `[&](){a;} >> [&](){b;}` (though of course there are lots of details left unspecified...)
- nickpsecurity 9y agoI never thought about shifting perspective on the semicolon from imperative to function. I'll have to revisit the idea after I learn functional programming. For now, maybe what you describe could be possible with tools that do C++ metaprogramming in Haskell or Racket like below. I liked what I was reading despite not even using those languages. ;) http://aszt.inf.elte.hu/~gsd/s/cikkek/abel/haskelltmp.pdf http://aszt.inf.elte.hu/~gsd/s/cikkek/abel/haskelltmp.pdf http://matt.might.net/papers/ballantyne2014metameta.pdf http://matt.might.net/papers/ballantyne2014metameta.pdf
- marcosdumay 9y ago> ie, in {a;b} what's the type of a ? Well, C++ is entirely run on IO, so `a :: IO a`. It's exactly the first argument of Haskell's `(>>)` when specialized into IO.
- barrkel 9y agoImagine each statement in a sequence is a function of the previous state to a subsequent state (+ optionally something extra); and the semicolon is a function of the previous state (+ something extra) and the statement function to the subsequent state (+ something extra). That's a description of the monad pattern in terms of semicolon overloading.