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
  • int
  • Example
  • string
  • Example
  • float
  • Example
  • bool
  • Example
  • atom
  • Example
  • pid
  1. Language Spec
  2. Classes and variables

Type conversions in simple variable types

Every simple variable type supports type conversion by invoking the constructor of the type.

int

Available for uint8, uint16, uint32/uint, uint64, int8, int16, int32/int and int64. When the conversion to an int fails, an exception is thrown.

Example

int("22") //is being converted to 22
int(1.5) //exception is thrown
int(true) //is being converted to 1

string

The string constructor accepts every type except for protos. Objects and structs are converted into JSON, other simple types are just being converted into strings (like with toString).

Example

string(21) //returns "21"
string(true) //returns "true"
string(22.3) //returns "true"

float

Available for float32/float as well as float64/double. When the conversion to a float fails, an exception is thrown.

Example

float("22") //returns 22.0
float("1.5") //returns 1.5
float(false) //returns 0.0
float(10) //returns 10.0

bool

When the conversion to a bool fails, an exception is thrown.

Example

bool(1) //returns true
bool("false") //returns false
bool(0.0) //returns false
bool("not a bool") //raises TypeConversionException

atom

Under the hood, the conversion to an atom will use toString. The conversion to an atom will only fail if one tries to convert a non-simple datatype.

Example

atom(1) //returns :1
atom(true) //returns :true
atom("ok") //returns :ok

pid

The only datatype that can be converted to a pid is an int. That is because the value of a pid is an int.

PreviousDeclaring arraysNextActors

Last updated 5 years ago