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
  • Ternary operator ? :
  • Example
  • Null-conditional operator ::
  • Example
  1. Language Spec
  2. Control flow

Ternary and nullcheck operators

Ternary operator ? :

Hades supports inline if (or ternary operator) statements. They work as such:

condition ? statementIfConditionIsTrue : statementIfConditionIsFalse

Example

with math from std:math

func sinc(x float)
    put x != 0.0 ? math.sin(x)/x : 1.0
end

Null-conditional operator ::

The null conditional operator checks if the left side of the operator evaluates to null. If it does, the statement on the right side of the operator is executed and returned.

Example

with exceptions from std:exceptions
with console from std:io

var doStuff = { x =>
    x :: raise exceptions.ArgumentNullException("{} is null".format(nameof(x)))
}

try
    doStuff(null)
catch(default e)
    console.out(e.message)
end

//Output: x is null

func add(a,b) requires (a :: false) and (b :: false)
    put a + b
end
with params from std:params
with exceptions from std:exceptions
with str from std:string
with Int from std:int

var a = params.get(0)
var b = params.get(1)

a :: raise exceptions.ArgumentNullException("{} is null".format(nameof(a)))
b :: raise exceptions.ArgumentNullException("{} is null".format(nameof(b)))

var number = a < b ? b : a
var numberFromString = Int.parse(value="This is not an integer", raise=false)
//one could also use int("This is not an integer")
var numberFromStringNullchecked = numberFromString :: 0
PreviousException handlingNextPipelines

Last updated 5 years ago