score:3
Accepted answer
By-name arguments are evaluated whenever they are mentioned. The spec says that right-associative operator method calls are evaluated like this:
a op_: b
desugars to:
{ val someFreshName = a; b.op_:(someFreshName) }
// ↑↑↑
// Eval happens here ↑↑↑
score:10
It's a bug. An old one, at that.
The linked pull request added a compiler warning when using the -Xlint
flag:
<console>:13: warning: by-name parameters will be evaluated eagerly when called as a right-associative infix operator. For more details, see SI-1980.
def :: (x: =>Int) = new Node(x) // a right-associative method
^
Source: stackoverflow.com
Related Query
- Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?
- Why does the Scala compiler fail with missing parameter type for filter with JavaSparkContext?
- Why does scala call the method passed to println before printing the line it is called in?
- Why does C# make the caller provide the actual parameter value for a method that has an optional parameter?
- When and why does the collect method fail for filtering Scala Collections by subtype?
- Why won't the Scala compiler apply tail call optimization unless a method is final?
- Why does this explicit call of a Scala method allow it to be implicitly resolved?
- Why does this Scala function compile when the argument does not conform to the type constraint?
- Why does the type parameter bound T <: Comparable[T] fail for T = Int?
- Why does Scala choose the type 'Product' for 'for' expressions involving Either and value definitions
- Why does a large array constructor call break the Scala compiler?
- Why does Scala compiler for .NET ignore the meaning of val?
- Why does scalaz's implementation of Monoid for Option evaluate the f2 function twice?
- Why does Scala implicit resolution fail for overloaded method with type parameter?
- Why does the Scala API have two strategies for organizing types?
- Why can method parameter F have the same name as type constructor F?
- Why the scala :_* to expand a Seq into variable-length argument list does not work in this case?
- Why does wrapping a method in another method stop type mismatch in Scala - using underscore in type parameter in pattern match?
- Why this simple Scala for comprehension does not execute the futures?
- Why BsonObjectId::apply(String) method does not work for the second time?
- Why does the Scala compiler give "value registerKryoClasses is not a member of org.apache.spark.SparkConf" for Spark 1.4?
- Why is the each iteration parameter val and not var in for loop in scala
- Why does Scala not allow a parameter name in a function type definition?
- Why does scalac take the Java vararg method instead of the single argument
- In Scala, why does a type annotation must follow for the function parameters ? Why does the compiler not infer the function parameter types?
- Why does the Scala compiler prefer to infer an argument of value null as Array[Char] instead of Object?
- Scala missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5)
- why does a scala class in the worksheet with same name but different case as the worksheet cause an exception?
- How to define a method for which the returntype is based on types of argument and type parameter in Scala?
- Why does the Scala worksheet evaluate the stream?
More Query from same tag
- Why do identical objects get different printed outputs in IntelliJ Scala REPL depending on how they were created?
- Split or extract a Value from a vector SCALA
- Using case class to add unknown columns as null
- Checking if elements of a tweets array contain one of the elements of positive words array and count
- Spark How to get number of Keys changed in two JSONS in Scala?
- Unzip a query of tuple in Slick+shapeless
- Context Bound on a Generic Class Using Implicits
- Splitting a scalaz-stream process into two child streams
- Is it possible to specify an anonymous function's return type, in Scala?
- Returning document as they are added with Scala reactivemongo
- Cosine similarity over tf idf output in spark dataframe (scala)
- (How) can I share a package on the main Scala Bazaar (scala-dev) with sbaz
- Can reduceBykey be used to change type and combine values - Scala Spark?
- I can define a bang operator method, but I can't call it in Scala. Why?
- Porting python-twisted based code to scala: framework advice needed
- Log parsing strategy using scala
- Scala- How can I read some specific bytes from a file?
- Finding how many times a given string is a substring of another string using scala
- What is RDD in spark
- Alternative to await.ready
- Add setter method to a scala class
- IntelliJ Bug: Scala 145, Error:scalac: Scala compiler JARs not found
- How does Scala attain parallelism?
- Recursive parsing based on level numbers
- Efficiently splitting Spark DataFrame into two with filtering only once
- Scala Tuples, Manipulating Map Data
- MergeSort in scala
- Lazy evaluation, thunk and function closure in Scala
- fruitless type test: a value of type Option[akka.actor.ActorSystem] cannot also be a akka.actor.ActorSystem
- Is there a way to use the '+' operator as addition on parameters passed to a method as a generic type in Scala?