How to define and declare Scala functions
In this issue, the editor will bring you about how to define and declare Scala functions. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Syntax:
Dcl:: = "def" FunDcl
FunDcl:: = FunSig: Type
Def:: = "def" FunDef
FunDef:: = FunSig [":" Type] "=" Expr
FunSig:: = id [FunTypeParamClause] ParamClauses
FunTypeParamClause:: = "[" TypeParam {"," TypeParam} "]"
ParamClauses:: = {ParamClauses} [nl] "("implicit" Params ")"]
ParamClause:: = [nl] "(" [Params] ")"}
Params:: = Param {"," Param}
Param:: = {Annotation} id [":" ParamType]
ParamType:: = Type
| | "= >" Type |
| | Type "*" |
The function declaration has the form: def f psig: T.J. F is the name of the function, psig is the parameter signature, and T is the return type. The function definition def f psig: t = e also includes the function body e, such as an expression that defines the result of the function. The parameter signature consists of an optional type parameter clause [tps], followed by zero or more value parameter clauses (ps1). (psn) composition. Such a declaration or definition introduces a value that has a (possibly polymorphic) method type whose parameter type and return type have been given.
The type of the given function body is expected to be consistent with the return type declared by the function. If the function definition is not recursive, the return type can be omitted because it can be inferred from the type packaged by the function body.
The type parameter clause tps consists of one or more type declarations in which type parameters that may have boundaries are introduced. The field of the type parameter includes the entire signature, as well as any type parameter boundary and the function body, if any.
The value parameter clause ps consists of zero or more canonical type bindings, such as x: t, that bind value parameters and associate them with their types. The scope of a canonical value parameter named x is the function body (if any). All type parameter names and value parameter names must be different.
The above is how to define and declare the Scala function. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.