2

Generally we use . for objects. What is this type of notation? How we are able to achieve the correct result from this?

NeoWelkin
  • 332
  • 1
  • 3
  • 12
  • In Scala, `+` is a method name, not the operator. – Shankar Jun 20 '17 at 07:02
  • Can not understand your question.... 1 here is an Integer, an Object , `+` a method of Integer, and what??? – John Zeng Jun 20 '17 at 07:02
  • @JohnZeng I read somewhere that Scala has rich wrapper library for every object.Rich Int for Int,Rich Float for float and so on. So here ,is that what happening exactly? – NeoWelkin Jun 20 '17 at 07:11

6 Answers6

10

I am copying and pasting text from "Programming in Scala," which says:

Scala doesn't technically have operator overloading, because it doesn't actually have operators in the traditional sense. Instead, characters such as +, -, *, and / can be used in method names. Thus, when you typed 1 + 2 into the Scala interpreter in Step 1, you were actually invoking a method named + on the Int object 1, passing in 2 as a parameter. As illustrated in Figure 3.1, you could alternatively have written 1 + 2 using traditional method invocation syntax, (1).+(2)

enter image description here

Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97
2

In Scala, 1 for example is an object of the type Int. This object has the method +, that accepts another Int as a parameter. Furthermore, Scala has some syntactic sugar that enables you to write 1.+(2) as 1 + 2, same as you could write foo.map(bar) as foo map bar.

EmilioMg
  • 376
  • 5
  • 14
  • got your explanation for my question. Can you explain the foo example.i am not from Java background. – NeoWelkin Jun 20 '17 at 07:13
  • `foo.map(bar)` is meant as a generic example or generalisation of this syntactic sugar. If you have an object `Foo` with the method `map`, which takes a parameter, e.g. `bar`, then you can write it `foo.map(bar)` or `foo map bar`. Just like it is with the `+`: `1 + 2` is internally converted to `1.+(2)` by the scala compiler. – EmilioMg Jun 20 '17 at 12:58
2

In Scala, also those which are known as "primitive types" in Java are objects, and extend AnyVal. In Scala, every value is an object which supports some methods, depending on its class. Numeric types as Int, Double, and so on have a method + for the sum (method names are not limited to alphanumeric characters). Now, the point is that Scala provides syntactic sugar that allow these method calls to appear as operations(operator syntax), hence 1+2 == 1.+(2)

metaphori
  • 2,681
  • 1
  • 21
  • 32
1

1 + 2 is a shorthand for 1.+(2) Here, + is the name of the method. Scala has no silly prejudice against non-alphanumeric characters in method names. You can define methods with just about any symbols for names.

In general, you can write a method b as a shorthand for a.method(b) where method is a method with two parameters (one implicit, one explicit). For example, instead of 1.to(10) you can write 1 to 10

Use whatever you think is easier to read. Beginning Scala programmers tend to stick to the Java syntax, and that is just fine. Of course, even the most hardened Java programmers seem to prefer a + b over a.+(b).

FaigB
  • 2,271
  • 1
  • 13
  • 22
0

From Documentation and Source File of Int class, you can get description for the + method and it's overloaded versions:

/** Returns the sum of this value and `x`. */
def +(x: Byte): Int
/** Returns the sum of this value and `x`. */
def +(x: Short): Int
/** Returns the sum of this value and `x`. */
def +(x: Char): Int
/** Returns the sum of this value and `x`. */
def +(x: Int): Int
/** Returns the sum of this value and `x`. */
def +(x: Long): Long
/** Returns the sum of this value and `x`. */
def +(x: Float): Float
/** Returns the sum of this value and `x`. */
def +(x: Double): Double

As you can see here, + looks like a method, nothing more. It is enough to understand the notation 1.+(2).

However, this is not the whole picture. See here for more information.

Trent Oh
  • 179
  • 2
  • 6
0

The below code is be self explanatory, on object int, i want to apply the methods. So i pressed . which gave me set of methods in which + is part of them, It takes int as the argument and returns Int. So it explains all.

The main reason is

Scala doesn't actually have operators in the traditional sense, but it has methods.

scala> val x=2;
x: Int = 2

scala> x.
!=   -    ==    ^           compareTo     intValue        isPosInfinity   isValidShort   round        toBinaryString   toFloat         toRadians   underlying
%    /    >     abs         doubleValue   isInfinite      isValidByte     isWhole        self         toByte           toHexString     toShort     until
&    <    >=    byteValue   floatValue    isInfinity      isValidChar     longValue      shortValue   toChar           toInt           unary_+     |
*    <<   >>    ceil        floor         isNaN           isValidInt      max            signum       toDegrees        toLong          unary_-
+    <=   >>>   compare     getClass      isNegInfinity   isValidLong     min            to           toDouble         toOctalString   unary_~

scala> x.+
                       def +(x: Long): Long   def +(x: Float): Float     def +(x: Int): Int    def +(x: Double): Double
def +(x: Short): Int   def +(x: Char): Int    def +(x: String): String   def +(x: Byte): Int

scala> x.+
                       def +(x: Double): Double   def +(x: Char): Int   def +(x: Short): Int   def +(x: String): String
def +(x: Long): Long   def +(x: Float): Float     def +(x: Int): Int    def +(x: Byte): Int

scala> x.+(3)
res1: Int = 5
Community
  • 1
  • 1
loneStar
  • 3,780
  • 23
  • 40