HadesLang Doc
  • Overview
  • Getting Started
    • Installing Hades
    • Basic Syntax
    • Coding Conventions
  • Language Spec
    • Foundation
      • Types
      • Built-in functions
    • Operators
      • Comparison and equality
      • Logical operators
      • Bitwise operators
      • Compound Assignment Operators
      • Operator overloading
    • Control flow
      • Conditions
      • Loops
      • Exception handling
      • Ternary and nullcheck operators
      • Pipelines
    • Classes and variables
      • Declaring variables
      • Declaring classes
      • Declaring structs
      • Declaring protos
      • Declaring arrays
      • Type conversions in simple variable types
    • Actors
      • Message passing
      • GenServer
      • Channels
    • Functions and lambdas
      • Declaring functions
      • Declaring lambdas
    • Functions of simple types
      • int
      • string
      • float
      • bool
      • atom
      • pid
    • Other
      • Annotations
      • Comments
      • Preprocessor statements
      • Reflection
      • Script file arguments
      • this keyword
  • Core Libraries
    • Standard library
      • std:io
        • console
        • file
        • directory
      • std:exceptions
      • std:int
      • std:internals
        • annotations
          • findMethod
          • findMethodInProto
          • findMethods
          • findMethodsInProto
        • function
      • std:collections
        • map
        • list
      • std:math
        • math
        • constants
        • matrix
      • std:networking
      • std:os
      • std:params
      • std:string
      • std:sql
    • Extra libraries
      • mssql:client
  • Other
    • Tools
    • Examples
    • Todos
Powered by GitBook
On this page
  • Declaring a simple lambda
  • Example
  • Declaring a complex lambda
  • Example
  • Lambda return types
  • Declaring a lambda without parameters
  • Example
  • Assigning a function to a lambda variable
  • Example
  1. Language Spec
  2. Functions and lambdas

Declaring lambdas

Declaring a simple lambda

The simple lambda consists of only a single statement. It will automatically return the result of said statement without the need of the put keyword.

Example

var add = {x,y => x + y}
add(2,2) //this would return 4

Declaring a complex lambda

A complex lambda contains multiple LOC and does require the use of the put keyword to return things.

Example

var pow = { x,y => 
    var result = 1
    
    while(y not 0)
        result *= x
        y--
    end
    
    put result //complex lambda needs a put statement
}

pow(4,2) //this would return 16

Lambda return types

Lambdas can state return and input types like so: lambda::(input types)->return type.

with console from std:io

var bye lambda::(string)->string = { a string => put "Bye " + a }
bye("Foo")

var cheers lambda::(string)->none = { a => console.out("Cheers " + a) }
cheers("Bar")

Declaring a lambda without parameters

If no parameter is needed, an _ is used as the parameter name, instead.

Example

with console from std:io
var hello = { _ => console.out:"Hello, world!" }
hello()

Assigning a function to a lambda variable

One can assign a function to a lambda. If the function has function guards, they are being rewritten into a match block internally. This can be useful when passing a function to a method.

Example

with console from std:io

func myFunction(a int) requires a < 10
    console.out("a is smaller than 10")
end

func myFunction(a int)
    //This default function is called when every condition is false
    console.out("a is " + a)
end

var fn lambda::(int)->none = myFunction

fn(1)  //Output: a is smaller than 10
fn(50) //Output: a is 50
with console from std:io

func onInit()
    console.out("Component initialized!")
end

func onError(e)
    console.outError("There was an error: {}".format(e))
end

...

comp.init(onInit, onError)
PreviousDeclaring functionsNextFunctions of simple types

Last updated 5 years ago