score:2
func
is type a => b
but it has a default value of "just return the a
part". so you can invoke foo
without an argument and it will use the default function.
score:0
this is called an optional parameter with a default value. basically,
def foo(x: int = 42) = ???
means: x
is of type int
and if the caller doesn't pass an argument, use 42
instead.
e.g.:
def foo(x: int = 42) = x
foo(23) //=> 23
foo() //=> 42
as opposed to:
def bar(x: int) = x
bar(23) //=> 23
bar() //error
score:1
as the others have written, the syntax is for a default parameter value, but i'd like to point out an interesting thing about its type. so the function takes a function from a to b and - if none provided it'll default to a basic identity function in a.
so how can a parameter of type a => b have a default value of type a => a?
strange as it may seem, but this compiles. calling it without a parameter passed however, will only compile if type parameters a and b are explicitly stated or inferred to be the same type.
so foo[string, int] should not, while foo[int, int] should compile if i'm not mistaken. i'm not sure what would happen about foo without explicit type params however, maybe a not very helpful [nothing, nothing] inferred?
Source: stackoverflow.com
Related Query
- what does this function signature mean?
- What does this mean - Unapplied methods are only converted to functions when a function type is expected
- What does an asterisk mean in function signature in scala?
- What does the tilde (~) mean in this Scala example?
- What does this import exactly mean in Scala?
- Scala: What does mean to pass a Set to the map function of a set
- What does "double right-arrow Type" with no LHS mean in function argument
- What does this piece of code mean in scala?
- scala switch case logic, what does otherDays mean in this code
- What does this block of Scala code mean
- What does this sign "<:" mean in scala?
- What does treating a method as a function mean in Scala?
- What does "String => String" notation mean when putting it as the return value of a function in Scala Language?
- What does code block mean in a scala anonymous function in Spark?
- what does optional function mean here?
- What does _ mean in this method which already takes a default value?
- What does this scala error mean : "type mismatch; found : (((Long, Long), Long),) required: (((Long, Long), Long)) ⇒ String"
- Scala , what does this function definition means?
- What does this line mean in slick?
- What does the parentheses syntax mean in this Table class?
- What does this Scala syntax mean (_*)?
- What does "coalgebra" mean in the context of programming?
- What does the `#` operator mean in Scala?
- What does param: _* mean in Scala?
- What does => and () => mean in Scala
- What does setMaster `local[*]` mean in spark?
- What does "<:" mean in Scala?
- What does a single apostrophe mean in Scala?
- What does the term "reason about" mean in computer science?
- What does "Forward reference extends over definition of value" mean in Scala?
More Query from same tag
- Field in a trait is not initialized in time
- How rename S3 files not HDFS in spark scala
- How to return the values of a query using the observer pattern
- Why has the symbol "/:" been deprecated
- Where is akka class Receive defined?
- How to install Scala plugin manually?
- Scala Pattern Matching Closures
- Calculating cosine similarity by featurizing the text into vector using tf-idf
- Find method in scala reflect by it's bytecode name
- What does it mean that 'lift is pretty much the same as map'?
- How to implement parent-children model with Akka in Scala?
- Scala : fold vs foldLeft
- How to run docker commands in Scala?
- verifying a probability distribution with variable arguments sums to 1
- Usage of spark DataFrame "as" method
- Implicit conversion where there resulting type is a type projection on a generic type
- Spark is duplicating work
- how to run gatling scenarios sequentially and not concurrently?
- Scala pattern matching with tuple: matching equal values in the tuple
- Generate all binary words of length n
- What is the idiomatic way of making a String from two Int in Scala?
- Conditional method chaining based on Option
- How to ssh with Amonite supplying the password in advance
- Is this the latest version of the maven scala plugin ?
- Libraries not visible to modules after SBT import in IntelliJ
- Scala: Type inference missing something
- Scala regular expressions: how to replace a BELL character (\007) in a string
- Expecting null in Scala. Should Option be used?
- How can I filter a result in akka?
- A Question on Type Classes