Search code examples
opa

Implementing callbacks in opa


I'm trying to implement callbacks in opa, so when a variable gets updated on the server side, clients get that updated value immediately.

I know I could do it with a network:

Network.add_callback(update_function, room)

however, as a learning exercise I would like to implement that feature myself.

My first thought was to create a list of void->void functions list(void->void), which is where I encounter my first problem in the lack of mutable state and variables. So I can't add any callback functions to the created list.

This leads me to my first question: How do I work with no mutable state? I don't consider myself new to functional programming, but normally I would just reassign a variable

i.e.

x = ['a','b','c']
function addVal(val){
    x = List.add(val,x)
}

But this doesn't seem to be legal in opa.

How is it possible to work without variables or mutable data structures? I don't see how any dynamic content can be implemented without mutable state.

I decided to try to get around the issue by storing my list of callback functions in a database:

database callbacks{
  list(void -> void) functions
}

but I get the error

Elements of type void -> void cannot be stored in the database

Which leads me to my second question:

Are there any better resources for opa available than doc.opalang.org? I haven't been able to find any information on the types that are allowed in a database.

So in summary:

  1. How do I build a program that actually does anything without the use of mutable state or variables?
  2. What types are allowed in opa databases?
  3. Are there any useful resources for opa other than doc.opalang.org?

Solution

  • 1) Opa is a functional language, Thus indeed the first example is not legal in Opa.

    x = ['a', 'b', 'c']
    

    Doesn't declare a variable x, but a binding between the ident x and the value ['a', 'b', 'c'].

    Even if Opa is a functional language, you have mutable as you see on the OpaDoc. But as you known mutable value are not thread safe, i.e. you can be preempted beetwen get and set, implies your sate is inconsistent.

    So to resolve this problem you can use Session. Basically a session is a state and a message handler. The session handler is thread safely executed to update the session state. Another advantage a session can be serialized and manipulates seamlessly by any system entity.

    Your example can be written :

    // A channel which can receive add message 
    channel({string add}) s = Session.make(['a', 'b', 'c'], 
          function(oldstate, message){
            match(message){
              case {add : value} : {set : List.add(value, oldstate)} //setting the new state
              .... // other message
            }
          }
        )
    
    function addVal(value){
      Session.send(s, {add : value}
    }
    

    2) You can store only purely data structure in the database.