3 ms·
Being able to use dots and parenthesis or not has a very good reason for being: It makes the concept of operators disappear. In a custom class, a + b is the sa
by hibikir 12y ago
Being able to use dots and parenthesis or not has a very good reason for being: It makes the concept of operators disappear. In a custom class, a + b is the same as a.+(b), instead of doing it the Java way, where blessed Java libraries can do things that are impossible for my own extensions. The C++ way of supporting such a thing relies on very specific sequences of what can be an operator, and what cannot, making things actually harder.
At the cost of making writing in-language DSL easier, like the one in ScalaTest, the language lets people do really ugly things, if they don't like other people reading their code. It's a tradeoff. I like to be able to have an assertion that goes " myVal should contain("A")", and that be valid ScalaTest code, with very good error handling, but I see how others can disagree.
Undescore is overloaded in a few more ways that you are listing too, as a wild card in case statements and type definitions. My favorite is how we can turn a sequence into a vararg by using the Punched-In-The-Face operator, :_. That nonsense really means that we are providing a type annotation (:), and the actual type to use should be whatever it is(_), but as a vararg (). If you know the exact type, you could go : Int, but really, :_ works the same way.
Now, as you go through Scala, the darkest realization is not that it has too many features, but that it's possible reach a point where you think it doesn't have enough. I have spent most of the week bemoaning the fact that shapeless' HLists are not anywhere near as easy to use as I'd like because the type system needs even more power.
- sagichmal 12y ago> I like to be able to have an assertion that > goes "myVal should contain("A")", and that > be valid ScalaTest code, with very good error > handling, but I see how others can disagree. This is so clearly a false economy! What you gain there in expressiveness you lose tenfold in ambiguity — and not just in that line, but in the latticework of exceptions you now have to keep suspended in your mind as you read every other line of code. It just doesn't make sense to me that you'd prefer things that way.