Arrow Functions
Harneet supports ES6-style arrow functions with TypeScript-like type annotations, providing a concise and type-safe syntax for writing functions.
Syntax
Single parameter (untyped)
If there is only one parameter without type annotation, the parentheses are optional.
| Single Parameter |
|---|
| package main
var double = x => x * 2
|
Single parameter (typed)
For typed parameters, parentheses are required along with the type annotation.
| Single Parameter Typed |
|---|
| package main
var double = (x int) int => x * 2
|
Multiple parameters (untyped)
If there are multiple parameters without types, the parentheses are required.
| Multiple Parameters |
|---|
| package main
var add = (a, b) => a + b
|
Multiple parameters (typed)
Typed arrow functions support full parameter and return type annotations.
| Multiple Parameters Typed |
|---|
| package main
var add = (a int, b int) int => a + b
var concat = (s1 string, s2 string) string => s1 + s2
|
Avoid using return
If you are using an inline arrow function, and you are using return it will raise an error. For example the following will create an error
| Invalid Return in Inline Arrow |
|---|
| var double = x => return x *2
|
If you still want to use return, you are encouraged to use
block body variant. In block body you must use return to return values.
Block body
For a multi-line function body, you can use a block body with an explicit return statement.
| Block Body Untyped |
|---|
| package main
var complex = (x, y) => {
var result = x + y
return result * 2
}
|
| Block Body Typed |
|---|
| package main
var multiply = (a int, b int) int => {
var result = a * b
return result
}
|
Examples
Basic Examples
| Basic Untyped Examples |
|---|
| package main
import fmt
var double = x => x * 2
fmt.Println(double(5)) // Output: 10
var add = (a, b) => a + b
fmt.Println(add(5, 10)) // Output: 15
var complex = (x, y) => {
var result = x + y
return result * 2
}
fmt.Println(complex(5, 10)) // Output: 30
|
Typed Arrow Functions
| Typed Examples |
|---|
| package main
import fmt
// Simple typed arrow function
var add = (a int, b int) int => a + b
fmt.Println(add(5, 3)) // Output: 8
// String operations
var concat = (s1 string, s2 string) string => s1 + s2
fmt.Println(concat("Hello", " World")) // Output: Hello World
// Boolean returns
var isPositive = (x int) bool => x > 0
fmt.Println(isPositive(5)) // Output: true
fmt.Println(isPositive(-3)) // Output: false
// Block body with types
var multiply = (a int, b int) int => {
var result = a * b
return result
}
fmt.Println(multiply(4, 7)) // Output: 28
|
Higher-Order Functions
Arrow functions work seamlessly as function parameters:
| Higher-Order Functions |
|---|
| package main
import fmt
function applyOperation(a int, b int, op function(int, int) int) int {
return op(a, b)
}
var addFunc = (x int, y int) int => x + y
var multiplyFunc = (x int, y int) int => x * y
fmt.Println(applyOperation(10, 5, addFunc)) // Output: 15
fmt.Println(applyOperation(10, 5, multiplyFunc)) // Output: 50
|
Mixed Typed and Untyped
You can mix typed and untyped parameters (though typed parameters are recommended for better type safety):
| Mixed Parameters |
|---|
| package main
import fmt
var process = (x int, y) int => x + y
fmt.Println(process(10, 5)) // Output: 15
|
Best Practices
- Use type annotations for better type safety and documentation
- Keep arrow functions concise - if the body is complex, consider using a regular function
- Use expression bodies for simple transformations
- Use block bodies when you need multiple statements or explicit returns
- Type your parameters when using as function arguments for better compile-time checks