-
Notifications
You must be signed in to change notification settings - Fork 126
Reference card
Your first Opa program:
-
Get Opa from its web-page.
-
In a
hello.opa
file, write:Server.start(Server.http, { title: "Hello" , page: function() { <>Hello web</> } } )
-
Compile (yes, Opa is a compiled language) with:
opa hello.opa
-
Run with:
./hello.js
-
Test by opening http://localhost:8080 in your browser.
TIP: You can also combine steps 3 and 4 with:
opa hello.opa --
Opa supports both single and multi-line comments.
// This is a single line comment. It goes until the end of the line
/* this is a
multi-line comment,
which must be closed with: */
Opa features almost complete type-inference so often, especially at the prototyping phase, you can write your programs without explicitly defining any types and they will be inferred by the compiler. However, you'll learn to appreciate the value of defining explicit types for important notions in your program: it greatly improves readability of your programs (serving as a documentation of sorts) and also allows Opa to produce much more readable and accurate error information.
// type abbreviation
type age = int
// functional types
/* meaning: a function taking two arguments: the first one is an int, the
second one is a string. It returns an integer */
type binary_fun = int, string -> int
// record types
type person = { string name, int age }
// parametrized (polymorphic) types
/* meaning: a binary operator of a parametric type */
type operator('ty) = 'ty, 'ty -> 'ty
// variant (sum) types
type boolean = {true} or {false}
// equivalent to:
/* i.e. we can omit the type if it's void, in which case the record
field just acts as a label (carries no information, except for its presence). */
type boolean2 = {void true} or {void false}
// parametrized variant type
type option('a) = {'a some} or {none}
// recursive type
type list('a) = {nil} or {'a hd, list('a) tl}
type bin_tree('a) = {'a leaf} or {bin_tree('a) left, bin_tree('a) right}
// function declaration
function int incr(int x) {
x + 1
}
// type annotations for arguments and results can be omitted
function incr_alt(x) {
x + 1
}
// function with local bindings and using tuples for arguments
function distance((x1, y1), (x2, y2)) {
dx = x1 - x2
dy = y1 - y2
Math.sqrt_f(dx*dx + dy*dy)
}
// anonymous function
function(x) { x + 1 }
// mutually recursive functions; 'recursive' and 'and' only
// needed for *local* functions (not needed at top-level)
recursive function odd(x) { if (x == 1) true else even(x-1) }
and function even(x) { if (x == 0) true else odd(x-1) }
The following modifiers can alert the declaration of the function in some way.
// Visibility modifiers:
private function priv_fun() {...} // function is invisible outside of its module
public function pub_fun() {...} // function is visible from other modules
// Distribution modifiers:
server function server_fun() {...} // function will be on the server
client function client_fun() {...} // function will be on the client
// Security modifiers:
exposed function exposed_fun() {...} // marks an entry-point (function can be called from the client)
protected function protected_fun() {...} // function should not be exposed to the client
Strings are primitive types in Opa.
String literals are constructed using double quotes "..."
. Please note that single quotes '...'
are not allowed for strings in Opa.
string s = "This is a string"
String concatenation are done with the ^
operator, or +
operator. See further below for more information about string manipulation functions.
string hello = "Hello" ^ " world"
string hello = "Hello" + " world"
A particular feature of Opa are inserts, which you'll use a lot. They allow to insert values into a string using curly braces.
// hence:
"{x} + {y} = {x+y}"
// is equivalent to:
x ^ " + " ^ y ^ " = " ^ (x + y)
Just below, you'll see that Opa also features HTML inserts.
XHTML is a defined not a primitive type in Opa, but there is a built-in syntax support for constructing XHTML values.
Here we only present an overview of the syntax used for XHTML:
type xhtml = ...
// XHTML is a data-type with built-in syntax
xhtml span = <span class="test">Hello XHTML</span>
// named closing tag is optional, so are the quotes around literal attributes
another_span = <span class=test>Hello XHTML</>
Inserts work in XHTML too (both for tags & attributes)
function mk_span(class, body) {
<span class="{class}">
{body}
</span>
}
DOM (Document Object Model) is a representation of X(HT)ML. You will often interact with the DOM to dynamically change the content of the page.
Below we just present few syntactical goodies that make it easier to make some most common transformations.
// manipulating (replacing/prepending/appending) DOM content for a given ID
#toto = <h1>Replace</h1>
#toto += <h1>Prepend</h1>
#toto =+ <h1>Append</h1>
// manipulating DOM selection
dom_pointer = Dom.select_children(#toto)
*dom_pointer += <h2>Before</h2>
While working with Opa you'll use records a lot. Therefore it's probably a good idea to get familiar with their syntax right away.
// full record construction
john = { name: "John Smith", age: 31}
// accessing record fields
john_string = "Name: {john.name}, Age: {john.age}"
// tilde-shortcut
name = ...
age = ...
// '~field' within the record abbreviates 'field: field'
person = { ~name, ~age }
// one can even also abbreviate all fields by putting tilde in front of the record
person2 = ~{ name, age }
// both 'person' and 'person2' are equivalent to
person3 = { name: name, age: age }
// record extension
// meaning: take record 'john' and replace its 'age' field with the given value
older_john = { john with age: john.age + 1 }
[Note] You need to re-assign the result of the record extension to a variable.
Pattern matching is used to analyze values that may take several variants.
// pattern matching on boolean values
bool x;
match (x) {
case {true}: ...
case {false}: ...
}
// pattern matching on a record representing an URL
match (url) {
// record field path is an empty list
// '...' indicates there may be more fields in the record
case {path:[] ...}: show_root()
// any path, bound to 'path' identifier
case {path:path ...}: show_at(path)
// equivalent to the above case
case {~path ...}: show_at(path)
// default case
default: show_root()
}
In Opa the database is tightly integrated in the language. At the moment the compiler supports 2 DB backends:
- MongoDB: http://www.mongodb.org/
- Dropbox-as-a-database
One can choose the back-end with the --database {mongo/dropbox}
compiler switch. Combining two different backends in one program is possible. By default, Opa will use Mongo backend.
Declare a collection of database values called person
database person {
// it contains an integer value 'age'
int /age
// a float value 'weight'
float /weight
// and a string value 'name'
string /name
}
The primitive values can be read with /db_name/field_name
notation
function string present_person() {
"Hey, my name is {/person/name}, I'm {/person/age} years old and I weigh {/person/weight} kg."
}
You can simply set a value:
/person/age <- 37
/person/weight <- 76.5
/person/name <- "John Doe"
You can also increment or decrement integer values.
/person/age++
/person/age -= 20
Database declaration can also include records
type person = { int age, string name }
database people {
person /me
}
We can read the whole record
person myself = /people/me
or just a chosen field
int my_age = /people/me/age
Similarly we can update the whole record
/people/me <- { age: 27, name: "Unknown" }
or just some of its fields:
/people/me/name <- "John Doe"
/people/me/age++
Lists are in fact just records, but there are few special syntactical goodies for operating on them:
database cities {
list(string) /capitals
}
Overwrite an entire list
/cities/capitals <- ["Amsterdam", "New York City", "Paris"]
Remove first and last element of a list
// first one
string city1 = /cities/capitals pop // city1 == "Amsterdam"
// last one
string city2 = /cities/capitals shift // city2 == "Paris"
Adding elements:
// Append one element
/cities/capitals <+ "Tokyo"
// Append several elements
/cities/capitals <++ ["Mumbai", "Delhi", "Shanghai"]
Removing elements:
// Remove one element
/cities/capitals <--* "Tokyo"
// Remove several elements
/cities/capitals <-- ["Mumbai", "Delhi"]
After all those operations we have:
/cities/capitals == ["New York City", "Shanghai"]
Sets and maps are very powerful concepts allowing to organize better and query data.
Let us begin with declaring a set of persons
type user_status = {regular} or {premium} or {admin}
type user = { int id, string name, int age, user_status status }
database users {
user /all[{id}]
// the status field is user-defined so we need to specify the default value
/all[_]/status = { regular }
// or to indicate that we will only manipulate full-records
/all[_] full
}
The [{id}]
after the path /all
indicates that we are declaring a set and not a single user
value and the {id}
value indicates that id
field will be the primary key.
To illustrate maps we will just use a simple abstract example with a map from int
s to string
s.
database map {
intmap(string) /m
}
We can fetch a single value from a set with a given key:
key = {id: 123}
user user1 = /users/all[key]
// equivalent abbreviation
user user1 = /users/all[{id: 123}]
Similarly for maps:
string v = /map/m[123]
For an overview of querying syntax and options you may refer to the relevant chapter in the manual. Here we will just provide some examples.
Examples for sets:
user /users/all[id == 123] // accessing single entry by its primary key
dbset(user, _) john_does = /users/all[name == "John Doe"] // return a set of values
dbset(user, _) underages = /users/all[age < 18]
dbset(user, _) non_admins = /users/all[status in [{regular}, {premium}]]
dbset(user, _) /users/all[age >= 18 and status == {admin}]
dbset(user, _) /users/all[not status == {admin}]
// showing second 50 results for users that are below 18 or above 62,
// sorted by age (ascending) and then id (descending)
dbset(user, _) users1 = /users/all[age <= 18 or age >= 62; skip 50; limit 50; order +age, -id]
Examples for maps:
string /map/m[123] // unique map association
intmap(string) /map/m[< 123 and > 456] // a sub-map for keys below 123 and above 456
Complete, single-value update:
/users/all[id == 123] <- {name: "John Doe", age: 32, status: {regular}}
// declaring file's package
package mlstate.tutorials.refcard
// importing other packages
import stdlib.web.mail
// more than one package at once
import stdlib.widgets.{button, dateprinter}
// all sub-packages
import stdlib.apis.facebook.*
// declaring a module
module ModuleA {
function fooh() {
...
}
}
// calling functions from another module
module ModuleB {
function bar() {
ModuleA.fooh();
...
}
}
// nested modules
module OuterModule {
private module InnerModule {
...
}
}
Log messages
// Add logs with Log.debug/info/notice/warning/error/fatal
Log.info("event_type", "debug msg, x={x}, v={v}")
/* Note that the log will either appear in the browser (use Development Console to see it)
* or in the terminal where you executed your server,
* depending on where the code with the debug command is executed.
* The terminal will only display log higher than notice by default.
* All logs go in error.log file.
*/
It not necessary to compile applications to tweak with resources (CSS) etc. Instead you can:
- compile your app in development mode (without
--compile-release
switch); - run it with the
-d
switch (or--debug-editable-css
or similar); -
opa-debug
directory will be created with app resources; - edit them and see the changes in your app immediately;
- Remember: when you're finished you still need to update app resources in their respective directory! (so that changes are kept when you recompile then app)
// type definition
type bool = {false} or {true}
// conditionals
if (b) { ... } else { ... }
// pattern matching
match (b) {
case {true}: ...
case {false}: ...
}
// literal -- the decimal dot makes it a float, not an int
float f = 10.
// operations
distance = Math.sqrt_f(dx*dx + dy*dy)
// conversion to int
int x = Float.to_int(f)
// conversion from int
float f = Float.of_int(17)
// conversion to string
string s = Float.to_string(3.14159)
// or simply with inserts
string s = "Value of f is: {f}"
// conversion from string
option(float) f = Parser.try_parse(Rule.float, "3.14159")
// literal
int i = 10
// conversion to float
float f = Int.to_float(i)
// conversion from float
int i = Int.of_float(3.14159)
// conversion to string
string s = Int.to_string(42)
// or simply with inserts
string s = "Value of i is: {i}"
// conversion from a string
option(int) i = Parser.try_parse(Rule.integer, "42")
// literal
string s = "This is a string"
// concatenation
s = s1 ^ s2
s = s1 + s2
// or with inserts
s = "Hey, {name}, nice to meet you!"
// length
String.length("Hello") == 5
// n'th character
String.get(1, "Hello") == "e"
// dividing at a separator
String.explode(",", "1,2,3") == ["1", "2", "3"]
// flattening a list of string
String.flatten(["1", "2", "3"]) == "123"
See also: String.capitalize, String.get_prefix, String.get_suffix, String.has_prefix, String.has_suffix, String.init, String.lowercase, String.print_list, String.replace, String.reverse, String.substring, String.trim, String.uppercase
Optional value of any type -- a type-safe approach to null-values.
// type definition
type option('a) = {'a some} or {none}
// construction
opt1 = none
option(string) opt2 = some("Hello")
// inspecting values (pattern matching)
match (opt) {
case {none}: ...
case {some: value}: ...
}
// default value; opt of type option(string)
string s = opt ? "default"
See also: Option.map, Option.switch
Lists in Opa are homogeneous, i.e. all elements must have the same type. It's the simplest container and you will use lists a lot.
// definition
type list('a) = {nil} or {'a hd, list('a) tl}
// construction
list(int) few_primes = [2, 3, 5, 7]
// concatenation
l12 = l1 ++ l2
// adding element 'elt' at the beginning of list 'l'
{hd: elt, tl: l}
[elt | l] // equivalent
List.cons(elt, l) // also equivalent
// list length
List.length([2, 4]) == 2
// pattern matching
match (l) {
case []: ... // list empty
case [hd | tl]: ... // first element 'hd' followed by list 'tl'
}
// modifying all elements (increasing by 1)
List.map(function(elt) { elt + 1 }, [2, 4]) == [3, 5]
// aggregating elements (multiplying all elements)
List.fold_left(function(acc, elt) { acc*elt }, 1, [2, 4]) == 8
// sorting a list
List.sort([4, 5, 2]) == [2, 4, 5]
// calling a function for every element of the list
List.iter(function(elt) { Log.info("ELT", "{elt}") }, [2, 4])
// check if element belongs to the list
List.mem(3, [6, 3]) == true
// converting to string
List.to_string([1, 3, 5]) = "[1, 3, 5]"
See also: List.assoc, List.exists, List.filter, List.find, List.flatten, List.get, List.init, List.nth, List.remove, List.rev.
Maps (dictionaries, hashmaps) are immutable structures mapping keys to values. They are comparable to hash-tables in other languages.
// types
type map('key, 'val) // map from 'key's to 'val'ues with default ordering
type stringmap('t) // map from strings to type 't
type intmap('t) // map from ints to type 't
// construction
stringmap(int) map = Map.singleton("ten", 10) // a map from strings to ints
intmap({string name, int age}) s = Map.empty // a map from ints to records
// conversion from list
stringmap(int) map = Map.From.assoc_list([("one", 1), ("six", 6)])
// conversion to list
Map.To.assoc_list(map) == [("one", 1), ("six", 6)]
// extending maps
new_map = Map.add("six", 6, map)
// checking if an element belongs to a map
if (Map.mem("seven", map)) { ... } else { ... }
// getting an element from a map
option(int) v = Map.get("seven", map)
Iterators: fold
, map
, iter
, filter
-- similar to lists.
Sets are containers holding and allowing to manipulate a number of elements of the same type.
// types
type set('elem) // set with elements of type 'elem
type intset // set of integers
type stringset // set of strings
// construction
stringset names = Set.singleton("John Smith")
// from list
Set.From.list([1, 4, 7])
// to list
Set.To.list(names) == ["John Smith"]
// extending sets
Set.add("Joe", names)
// checking if an element belongs to a set
if (Set.mem("Dave", set)) { ... } else { ... }
// getting an element from a set
option(int) v = Set.get("Alice")
Iterators: fold
, map
, iter
-- similar to lists and maps.
This module allows one to declare Servers, i.e. entry-points of Opa programs. One declares a server with Server.start
that gets two parameters: configuration and a handler defining how to map URIs to resources.
// Start a server defined by 'handler' with configuration 'conf'
Server.start(Server.conf conf, Server.handler handler)
/* Configurations (1st argument of Server.start) */
Server.http // default HTTP config (on port 8080)
Server.https // default HTTPS config (on port 8080)
{port: 80, netmask:0.0.0.0, encryption: {no_encryption}, name:"my server"} // custom server
/* Handlers (2nd argument of Server.start) */
function mypage() {
<>Hello web!</>
}
// single page server (URI ignored, always the same page)
{ page: mypage
, title: "My app"}
// multi-page server, function from URI (string) to a resource
dispatcher = parser
| "/" -> Resource.page("Hello", <>Hello web</>)
| "/_rest_/" .* -> Resource.page(...)
| .* -> Resource.page(...)
{ custom: dispatcher }
// multi-page server, function from URI (structured) to a resource
// + possibility to add a filter to decide which URIs to handle
function start(url) {
match (url) {
case {path:[] ... }: ...
case {~path ...}: ...
}
}
{ dispatch: start }
// simple server for *serving* a bundle of resources
{ resources: @static_resource_directory("resources") }
// no request handling but registering a list of custom resources (JS/CSS)
{ register: {css: ["resources/css/style.css", "resources/js/myjs.js"]} }
// One can also use a list of servers.
// For instance bundle + custom resources + dispatcher).
// Helpful for building servers in a structured way
// Servers are tried in the given order and the first successful will serve the request
Server.start(
Server.http,
[ {resources: @static_resource_directory("resources")} // serve custom resources
, {register: {css: ["resources/css.css"]}} // use custom CSS
, {title: "Chat", page:start } // serve the one-page app!
]
)
In Opa XHTML is a data-type, with special syntax support.
See syntax introduction for more information.
page = <span class="hello">Hello web</span>
// name in the closing tag is optional
// so are the quotes for attributes
page = <span class=hello>Hello web</> // equivalent
// you can pass XHTML as arguments to functions
// and use it with inserts
function block(title, content) {
<div class=block>
<span class="{class}">
</span>
</div>
}
// just like XHTML, CSS is a data-type in Opa
red_style = css { color: red }
span = <span style={red_style} />
// one can use inserts inside css
function div(width, height, content) {
<div style={ css { height: {height}px; width: {width}px }}>
{content}
</div>
}
// one can also register external css (from a separate project file)
Server.start(Server.http,
[ {register: {css: ["resources/css/style.css"]}}
, ...
])
See syntax introduction for some introduction to DOM manipulation.
// get a fresh ID
Dom.fresh_id()
// DOM selections
#test // element with ID "test"
#{test} // element with ID equal to (string) variable 'test'
Dom.select_document() // complete document
Dom.select_class(class) // all elements with class 'class'
// and much more...
// DOM effects
Dom.transition(#some_id, Dom.Effect.fade_out())
Dom.transition(dom_selection, Dom.Effect.slide_in())
For more effects and ways of applying them see the: Dom.Effect module.
// URI definition:
type Uri.uri = Uri.absolute or Uri.relative or Uri.mailto
// where Uri.relative is:
type Uri.relative =
{ list(string) path
, option(string) fragment
, list((string, string)) query
, bool is_directory
, bool is_from_root
}
// When constructing a server you can dispatch pages by their relative URIs:
function dispatch(url) {
match (url) {
case {path:[] ... }: ...
case {path:["_rest_" | _] ...}: ...
default: ...
}
}
Server.start(Server.http, { dispatch: start })
Resources are objects that can be served by the server to the clients, such as web pages, images, scripts, stylesheets etc.
// Constructing resources
Resource.page("Page title", <span>Page body</>) // HTML page
Resource.styled_page(title,
["resources/style.css"], body) // HTML page + custom CSS
Resource.image({png:
@static_source_content("./resources/index.jpg")}) // image
Resource.raw_status({wrong_address}) // HTTP response code
// Registering custom CSS resource (app-wide)
Resource.register_external_css(url)
// Registering custom JS resource (app-wide)
Resource.register_external_js(string url)
// You will provide Resources in the server in response to URLs
function start(url) {
match (url) {
case {path:[] ... }: Resource.page("Home", home())
case {~path ...}: Resource.page("Some page", gen_page(path))
}
}
Server.start(Server.http, {dispatch: start})