-
Notifications
You must be signed in to change notification settings - Fork 126
Frequently Asked Questions
We just started this FAQ wiki page! This page can be edited by everyone, don't hesitate to contribute!
To learn more about Opa, check out the Opa Documentation, the API and the Reference Card.
I have a record representing a user, for example:
baby = { name : "Alice", age : 1 };
I tried this to change the age, but it doesn't work:
baby.age = 21
Solution:
adult = { baby with age : 21 }
In an imperative language, where it would be valid to do this, it means the "baby" you defined at the beginning of your code can have its age changed elsewhere in the code: the object you would manipulate in the future has suddenly nothing to do with a baby and you have no clue it has changed. With a functional language you have the guarantee a value you defined can't be affected by other external function.
I have this initial users intmap:
users = IntMap.empty
My map is still empty after this:
IntMap.add(1, "Alice", users)
Solution:
users = IntMap.add(1, "Alice", users)
Understand why reading in the functional language section. Some related discussion about maps can be found here:
- http://stackoverflow.com/questions/10586367/opa-howo-to-manipulate-stringmap-and-other-map
- http://stackoverflow.com/questions/10511445/opa-iterating-through-stringmap-and-forming-a-new-string-based-on-it
I understand Opa is a functional programming language, but what if I really need to do side effects on my values, for example to write an imperative algorithm?
Use the Mutable module:
m = Mutable.make(0);
m.get(); // => 0
m.set(1);
m.get(); // => 1