Why is the .+() method deprecated in scala? - scala

Just wondering why I'm seeing the following in my REPL:
scala> 5.+(9)
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
res18: Double = 14.0

If you follow the instructions and run the REPL using scala -deprecation, you'll see the reason for the deprecation:
scala> 1 + 1
res0: Int = 2
scala> 1.+(1)
<console>:1: warning: This lexical syntax is deprecated. From scala 2.11,
a dot will only be considered part of a number if it is immediately followed
by a digit.
1.+(1)
^
res1: Double = 2.0
Tested on Scala 2.10.1

Related

Set.map now returns a Set warning scala

I am getting this warning on mapping on sets, but I want to maintain the set output. How do we go about fixing this? I do not want to suppress the warning.
warning: method map in trait SetLike has changed semantics in version 2.8.0:
Set.map now returns a Set, so it will discard duplicate values.
someSet.map(_.fullName)
signature
def mapSet(someSet: Set[People]): Set[String]
Using Scala v2.7 REPL:
scala> scala.collection.mutable.Set("a","aa","b").map{x => x.size}
res0: Iterable[Int] = ArrayBuffer(1, 1, 2)
Using Scala v2.8 REPL:
scala> scala.collection.mutable.Set("a","aa","b").map{x => x.size}
res0: scala.collection.mutable.Set[Int] = Set(1, 2)
This difference between Scala 2.7 and 2.8 mutable Set (precisely speaking SetLike) can change program semantics, so the migration warning is shown.

scala spark paritions: understanding the output

I am running spark and scala. What is the meaning of the line that i get when i run rawblocks.partitions.length? My linkage folder had 10 files.
what does res1 and Int stand for?
Also is there a place where i
can find official documentation for spark methods? For example i
want to see details of textFile.
spark version 1.6.1
Using Scala version 2.10.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65)
scala> val rawblocks=sc.textFile("linkage")
rawblocks: org.apache.spark.rdd.RDD[String] = linkage MapPartitionsRDD[3] at textFile at <console>:27
scala> rawblocks.partitions.length
res1: Int = 10
The res1 and Int are not special to Spark: res1 is a name given in Scala REPL (shell) to unnamed values - results are numerated (starting from zero), for example:
scala> 10
res0: Int = 10
scala> "hello"
res1: String = hello
This should also give you a clue about Int - it's the inferred type of this value (Scala's Int is somewhat equivalent to Java's Integer).
Spark API: here's the documentation for the two primary entry points of Spark-core: SparkContext, RDD

Can double literal end with a dot?

I'm migrating from Scala 2.9 to Scala 2.11.0-M5.
Following double field initialization with a constant floating point literal fails.
Code example:
class Test {
val okDouble = 0.0
val badDouble = 0.
val nextValue = 0
}
Scala interpreter error:
scala> class Test {
| val okDouble = 0.0
| val badDouble = 0.
| val nextValue = 0
<console>:4: error: identifier expected but 'val' found.
val nextValue = 0
The problem here is the dot at the end of badDouble definition.
Should 0.0 be always used for double literals now?
Double literals ending with dot were deprecated in Scala 2.10 and removed from the language in Scala 2.11:
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45).
scala> 3.
<console>:1: warning: This lexical syntax is deprecated. From scala 2.11, a dot
will only be considered part of a number if it is immediately followed by a digit.
3.
^

Adding an Int to a String in scala [duplicate]

This question already has answers here:
Scala operator oddity
(2 answers)
Closed 10 years ago.
I am having a little trouble understanding the rationale behind the following few pieces of scala code:
We all know that 1 + 1 = 2 in the REPL.
scala> 1 + 1
res0: Int = 2
If I type in "Abc" + "Def", I should get "AbcDef" in the REPL.
scala> "Abc" + "Def"
res6: java.lang.String = AbcDef
Now let's say I invoke the + method on String "Abc" and pass "Def" as a parameter:
scala> "Abc".+("Def")
res7: java.lang.String = AbcDef
By the same rationale, why does something like 1.+(1) return a double 2.0?
scala> 1.+(1)
res1: Double = 2.0
Also, why does passing the argument "1" as a parameter result in "1.01" as follows:
scala> 1.+("1")
res9: String = 1.01
Why is the returned result a String instead of an effort for "1" to me transformed into the callers type?
Thanks
If you try that on Scala 2.10.0, you'll get a clue as to the answer:
scala> 1.+(1)
<console>:1: warning: This lexical syntax is deprecated. From scala 2.11,
a dot will only be considered part of a number if it is immediately
followed by a digit.
1.+(1)
^
Simply put, 1. is a valid Double in Scala (as it is in Java), so Scala is really doing this:
1. + (1)
That is, infix addition of a Double to an expression enclosed inside a (redundant) parenthesis.
As for the latter, Scala follows Java convention that anything added to a String results in a String and vice versa.

Scala operator oddity

When I invoke + on 2 I get an Int back, but when its done using explicit method call I get Double instead.
scala> 2+2
res1: Int = 4
scala> 2.+(2)
res2: Double = 4.0
It seems that .+() is invoked on implicit converted Int to Double.
scala> 2.+
<console>:16: error: ambiguous reference to overloaded definition,
both method + in class Double of type (x: Char)Double
and method + in class Double of type (x: Short)Double
match expected type ?
2.+
^
Why is that so ?
In Scala 2.9 and before, 2. is interpreted as 2.0 so the ambiguous dot denotes a float literal. You’d explicitly call the method by using the syntax (2).+(2).
The ambiguous floating point syntax will however be deprecated in 2.10:
scala> 2.+(2)
<console>:1: warning: This lexical syntax is deprecated. From scala 2.11, a dot will only be considered part of a number if it is immediately followed by a digit.
2.+(2)
^
<console>:2: warning: This lexical syntax is deprecated. From scala 2.11, a dot will only be considered part of a number if it is immediately followed by a digit.
2.+(2)
^
<console>:8: warning: This lexical syntax is deprecated. From scala 2.11, a dot will only be considered part of a number if it is immediately followed by a digit.
2.+(2)
^
res1: Double = 4.0
The reason is not in explicit method call -- by writing 2.+ you specifying Double on the left side and then calling addition operator on it:
scala> 2.
res0: Double = 2.0