Enough with the abuse of Scala for internal DSLs!

I just read a comment in reddit where someone explained that Scala's feature that allows "subj.method(obj)" to be written as "subj metohd obj" is for writing DSLs.


(Disclaimer: I don't know what were Martin Odersky's exact reasons for adding this feature)


For me the primary reason is not DSLs. It is the simple fact that it makes the language more consistent. In Java, you can write 'x + y', but this is allowed only for special types (primitives and string). This is inconsistent. In scala, 'x + y' is allowed for all types.


Now naturally, 'x + y' is more readable than x.plus(y), for the simple fact that all major languages support it, rather than insisting on plus(x, y) or x.plus(y). So if I add a complex number type, or a rational number type, I'd want to be able to use 'x + y'. And what if I want a new operator, e.g., ^, why must I write power(x, y) instead of the more readable x^y? I can because scala allows me to define method names containing symbols.


So for mathematic like constructs this feature makes the language more consistent and code to be more readable (for examples that are not arithmetics, see scala's parser combinators). It can even be useful for alphanumeric method names (e.g, 'x or y')


However, people tend to abuse it to be able to write something like "obj must be equal to 2". Now this is fine as long as it is a "cute" one liner, but creating a whole DSL is plain wrong the reason is that this "DSL" must still conform to the scala syntax and so anyone using it must know Scala and then the superflous baggage that comes from trying to make such DSL is not worth the gains.


Bottom line: Scala's "subj meth obj" feature is great. Just use it where appropriate.

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com