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
  • if statement
  • Example
  • Example
  • match block
  • Example
  • Example
  1. Language Spec
  2. Control flow

Conditions

if statement

The if statement executes statements based on some conditions.

Example

with console from std:io

if(a < 10)
    console.out("a is smaller than 10")
else if(a is 11)
    console.out("a is 11")
else if(a > 11 and a < 21)
    console.out("a is greater than 11 and smaller than 21")
else
    console.out("a is " + a)
end

An if block can contain multiple else if block.

Example

with console from std:io

if(condition)
    console.out("yes")
else if(otherCondition)
    console.out("maybe")
else if(otherOtherCondition)
    console.out("maybe not")
else
    console.out("no")
end

match block

The match block is similar to a switch block in C languages. Match cases accept lambdas as actions. If multiple match cases evaluate to true, the first match case is invoked, except if specified otherwise (with match all).

Example

with console from std:io

let fruit = "Apple"
var action lambda = { _ => 
    console.print("Variable is of type string")
}

match all(fruit)
    "Apple" => console.out("Apples are really tasty!")
    fruit.type() is :string => action
end

/*
Output: 
Apples are really tasty!
Variable is of type string
*/

match(fruit)
    "Apple" => { _ => console.out("Apples are really tasty!")}
    fruit.type() is :string => action
end

//Output: Apples are really tasty!

The match block is also able to filter objects and lists.

Example

with console from std:io

let person = Person("John", "Doe")
let list = {:ok, "Hello world"}

func doMatch(a)
    match(a)
        Person{firstName: "John"} =>
            console.out("Hello, John") //single operations can be written like so
        {:ok, msg} => console.out(msg)
    end
end

doMatch(person) //Output: Hello, John
doMatch(list) //Output: Hello world
PreviousControl flowNextLoops

Last updated 5 years ago