-2

I am new to Scala coming from Java world. Scala seems to be more complex than Java. Moreover, I noticed that many Scala constructs use ambiguous methods names. Apart from +,-,*, and / that are pretty obvious what they are, is there any reason to use methods like this:

object ActorRef {
  implicit final class ActorRefOps[-T](val ref: ActorRef[T]) extends AnyVal {
    /**
     * Send a message to the Actor referenced by this ActorRef using *at-most-once*
     * messaging semantics.
     */
    def !(msg: T): Unit = ref.tell(msg) 
  }
}

When I see an exclamation mark sending a message is the first thing that comes to mind !

Another example:

List has a method named `:::' for list concatenation

Here's how you use it:

val oneTwo = List(1, 2)
val threeFour = List(3, 4)
val oneTwoThreeFour = oneTwo ::: threeFour
  println(oneTwo + " and " + threeFour + " were not mutated.")
  println("Thus, " + oneTwoThreeFour + " is a new list.")

Is there any technical or architectural reason for trying to use special characters for method names or it just a kind of "show off"?

Adelin
  • 18,144
  • 26
  • 115
  • 175
  • 1
    Akka was inspired by the actor model implemented in Erlang, where `!` is used to send messages between actors. I guess `!` was just chosen just to stick to the convention established by Erlang. – Krzysztof Atłasik Nov 05 '19 at 08:49
  • @KrzysztofAtłasik What about ::: , where it is inspired from? besides who cares where it is inspired from? Isn't the readability more important ... ? – Adelin Nov 05 '19 at 08:53
  • Operators usually make code very succinct, but overusing them can indeed make code less readable. Please note, that what is readable and what's not is very subjective. Some people just thought that `!` fits very well for sending messages since it was already used in other similar languages. – Krzysztof Atłasik Nov 05 '19 at 09:09
  • I would imagine that overuse of lots of different short names `::`, `:::`, `:+`, `+:`, `++`, `++:`, could make code hard to read. But then again I'd wonder if the code was mixing styles. Also bear in mind that `:` at the end means right associative https://stackoverflow.com/a/7889189/5986907 – joel Nov 05 '19 at 09:12
  • Not to mention some like ~> ~! and others. Coming from Java world I used to have discussions for over 20 - 30 min about choosing the best names for the methods and here comes Scala to put it in your face :D – Adelin Nov 05 '19 at 09:15
  • Relevant question and answers: https://stackoverflow.com/questions/1098303/what-makes-scalas-operator-overloading-good-but-cs-bad – jrook Nov 05 '19 at 09:26
  • Also see https://stackoverflow.com/questions/55620690/how-to-avoid-writing-confusing-dsls-in-scala – joel Nov 05 '19 at 09:46
  • Also note that some methods have more verbose pseudonyms, like `:/` and `foldLeft`. There the latter is encouraged – joel Nov 05 '19 at 09:50
  • 1
    "besides who cares where it is inspired from?" The point is that there are a significant number of Akka users for whom "When I see an exclamation mark sending a message is the first thing that comes to mind" is just true, no sarcasm. – Alexey Romanov Nov 05 '19 at 11:52

1 Answers1

4

Thats a good question.

In Scala, all operators you know and love (+, /, *, -) are also functions.

So you could rewrite the term:

val hornsOfUnicorn = 2 - 1

to

val hornsOfUnicorns = 2.-(1)

Try it out!

Logically, this makes sense, and is actually a feature that is missing from Java for historical reasons.

When you are allowed to use the full UTF-8 - range of characters for your functions (read: custom operators), why not make them less verbose and more concise and expressive?

P.S: The list syntax of :, :: and ::: originally comes from Haskell.

It is used to express the head-tail-nature of lists, in languages like Haskell, Scala or PureScript.

It actually describes alot more than "just an operator"; to understand this look at the ScalaDoc of List.

Markus Appel
  • 3,138
  • 1
  • 17
  • 46
  • why not make them less verbose and more concise and expressive? So that I don't miss readability. Regarding more concise and expressive, I think it makes them less concise and expressive. – Adelin Nov 05 '19 at 08:56
  • @Adelin I can see that you would think that, coming from Java. When used in moderation, it can greatly improve the code. When overused, I agree, it can be confusing. – Markus Appel Nov 05 '19 at 08:58
  • "The list syntax of :, :: and ::: comes from Haskell" But why do I need to keep old constructs coming from other languages, not mention that Haskel is barely used nowadays (https://www.tiobe.com/tiobe-index/) – Adelin Nov 05 '19 at 08:59
  • @Adelin "Haskell is barely used nowadays" - that is incorrect. It's used by Microsoft Reasearch as experimental compiler, for example. – Markus Appel Nov 05 '19 at 09:01
  • "It's used by Microsoft Reasearch as experimental compiler, for example" Any production system ? As Scala is trying to be a production system. – Adelin Nov 05 '19 at 09:05
  • Haskell is production ready. But to get back to the main argument: Would you always want to write `add(1, 2)` instead of `1 + 2`? Where do you draw the line? Who defines which functions are operators, and which have to be named? Oracle? Democratising operators is a good choice, and where you think operators end and functions begin is a matter of personal preference. – Markus Appel Nov 05 '19 at 09:19
  • @Adelin this thread is becoming off-topic. "Name a production system in Haskell" deserves a separate question. On another website. – simpadjo Nov 05 '19 at 09:40
  • 1
    @Adelin: Microsoft's Terminator is written in Haskell. Terminator is a tool which checks Windows Drivers for accidental infinite loops and is part of the Windows Driver Development Kit. The Glasgow Haskell Compiler is a production-quality, industrial strength compiler, written in Haskell. Deutsche Bank, Credit Suisse, Google, Facebook, Intel, Ericsson, Alcatel-Lucent, are just some of the other users. – Jörg W Mittag Nov 05 '19 at 09:47