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
  1. Language Spec
  2. Actors

GenServer

GenServers are a concept directly taken from Elixir.

A GenServer is a process like any other Elixir process and it can be used to keep state, execute code asynchronously and so on. The advantage of using a generic server process (GenServer) implemented using this module is that it will have a standard set of interface functions and include functionality for tracing and error reporting. It will also fit into a supervision tree.

From the Elixir hexdocs

with GenServer from std:sync
with console from std:io

class Counter < GenServer
  func! init(state)
    put {:ok, state}
  end
  
  //If you want to use function matching, you have to explicetly state this
  func! call(msg, from, state);
  
  //We don't need ! since we already stated it above
  func call(msg := :get, _, state)
    put {:reply, state, state}
  end
  
  func! cast(msg, state);
  
  func cast(msg := {:increment, number}, state)
    put {:noreply, state + number}
  end
end

try
  var {:ok, ctrPid} = GenServer.start(Counter, 0)
  
  send(ctrPid, {:increment, 10})
  var nr = GenServer.call(ctrPid, :get)
  console.out(nr) //Output: 10
catch(e)
  //ignored
end
PreviousMessage passingNextChannels

Last updated 5 years ago