def func(arg: String => Int): Unit = { // body of function }
I mean this snippet: String => Int
As mentioned in Advantages of the Scala s type system , this is a Functional Type .
In a Scala article for Java Refugees, Part 6: "Java Overloading" describes this syntax in the "Higher Order Functions" section.
def itrate(array:Array[String], fun:(String)=>Unit) = { for (i <- 0 to (array.length - 1)) { // anti-idiom array iteration fun(array(i)) } } val a = Array("Daniel", "Chris", "Joseph", "Renee") iterate(a, (s:String) => println(s))
? , ., fun, (type1, …)=>returnType, .fun , String Unit ( , )., . fun , , , .C/++ , . , , , , .
(type1, …)=>returnType
: def func(arg: String => Int): Unit, arg , String Int.
def func(arg: String => Int): Unit
arg
, Int
Scala . , ( ) .
() => Unit
, Unit (java void).
, Int:
(String) => Int
, scala , . arg: - .
arg:
func (arg) :
func
val result = arg("Some String") // this returns a Int
You can also see it written (possibly by a decompiler) as
def func(arg: Function1[String, Int]): Unit = { // body of function }
They are exactly equivalent.
Source: https://habr.com/ru/post/1751910/More articles:Is this valid JSON markup? - jsonCan I use Android scripts created with ASE as a typical Android application (for example, written in Java)? - androidUnderstanding classes in PHP - inheritanceAdd dataset issue to ASP.Net application - c #Powershell hash table issue - powershellvim displaying normal keys such as shift - vimHow to emulate a device in kvm - linuxUpload image using Flex and PHP - flexQuestion with heading C ++ - c ++Является ли CoreData, как правило, используемым в качестве модели, или это детализация инфраструктуры инфраструктуры? - design-patternsAll Articles