- Opa Application Development
- Li Wenbo
- 872字
- 2021-08-20 16:49:38
Basic datatypes
Datatypes are the shapes of data manipulated by an application. Opa uses datatypes to perform sanity and security checks on your application. Opa also uses datatypes to perform a number of optimizations. There are three basic datatypes in Opa: integers, floats, and strings. Also, you can define your type with the keyword type
:
type Student = {string name, int age, float score} Student stu = { name:"li", age:28, score:80.0}
Actually, thanks to a mechanism of type inference, Opa can work in most cases even if you do not provide any type information. For example:
x = 10 // the same as: int x = 10 x = {a:1,b:2} // the type of x is: {a:int, b:int}
So in the rest of this chapter, we will not address type information before variable, but you should know what type it is in your mind. In actual coding, a best practice is to provide the datatypes of our main functions and to let the inference engine pick up the datatypes of all the local variables and minor functions.
Integers
It is quite simple to write integer literals; there are a number of ways to do so:
x = 10 // 10 in base 10 x = 0xA // 10 in base 16, any case works (0Xa, 0XA, Oxa) x = 0o12 // 10 in base 8 x = 0b1010 // 10 in base 2
Note
The tailing semicolon is optional in Opa; you can add it if you want.
Opa provides the module Int
(http://doc.opalang.org/module/stdlib.core/Int) to operate on integers. The following are the most used functions:
i1 = Int.abs(-10) // i1 = 10 i2 = Int.max(10,8) // i2 = 10
There is no automatic type conversion between float
, int
, and String
. So, use the following functions to convert between int
, float
, and String
.
i3 = Int.of_float(10.6) // i3 = 10 i4 = Int.of_string("0xA") // i4 = 10, 0xA is 10 in dec f1 = Int.to_float(10) // f1 = 10.0, f1 is a float s1 = Int.to_string(10) // s1 = "10", s1 is a string
Floats
It is also easy to define floats. They can be written in the following ways:
x = 12.21 // the normal one x = .12 // omitting the leading zero x = 12. // to indicate this is a float, not an integer x = 12.5e10 // scientific notation
Opa provides the module Float
(http://doc.opalang.org/module/stdlib.core/Float) to operate on floats. The following are the most used functions:
f1 = Float.abs(-10.0) //f1 = 10.0 f2 = Float.ceil(10.5) //f2 = 11.0 f3 = Float.floor(10.5) //f3 = 10.0 f4 = Float.round(10.5) //f4 = 11.0 f5 = Float.of_int(10) //f5 = 10.0 f6 = Float.of_string("10.5") //f6 = 10.5 i1 = Float.to_int(10.5) //i1 = 10, i1 is an integer s1 = Float.to_string(10.5) //s1 = "10.5", s1 is a string
Strings
In Opa, text is represented by immutable utf8-encoded character strings. String literals follow roughly the same syntax used in C language, Java, or JavaScript. Note that you will have to escape special characters with backslashes.
x = "hello!" x = "\"" // special characters can be escaped with backslashes
Opa has a feature called string insertions, which can put arbitrary expressions into a string. You can do that by embedding an expression between curly braces into a string. For example:
x = "1 + 2 = {1+2}" //will produce "1 + 2 = 3" lang = "Opa" y = "I love {lang}!" //will produce "I love Opa!"
Opa provides the module String
(http://doc.opalang.org/module/stdlib.core/String) to operate on strings. The most commonly used are as follows:
s = "I love Opa! " //Note there is a space at the end. len = String.length(s) //get length, len = 12 isEmpty = String.is_empty(s) //test if a string is empty, false isBlank = String.is_blank(s) //test if a string is blank, false cont = String.contains(s,"Opa") //check if a string contains a //substring,true idx1 = String.index("love",s) //found, idx1 = {some:2} idx2 = String.index("loving",s) //not found, idx2 = {none} ch = String.get(0,s) //get nth char, ch = 'I' s2 = String.trim(s) //do trim, s2 = "I love Opa!" s3 = String.replace("I","We",s2)//s3 = "We love Opa!"
Sum
A value has a sum type t
or u
, meaning that the values of this type are either of the two variants, a value of type t
or a value of type u
.
A good example of sum type are Boolean values, which are defined as follows:
type bool = {true} or {false}
Thus, a variable of type bool
can be either {true}
or {false}
. Another commonly used sum type is the option
type, which is defined as:
type option('a) = {none} or {'a some}
The option(`a)
value is either none
or some
(a value x of type `a
). Type `a
means any type. This is a type-safe way to deal with possibly non-existing values. The option
type is widely used; let's take String.index
for example:
idx1 = String.index("love","I love Opa!") //idx1 = {some:2} idx2 = String.index("loving","I love Opa!") //idx2 = {none}
The return type of String.index
is the option (int
), which means it will return a {some:int}
record if a substring appears or a {none}
record if it doesn't.
Note that the sum datatypes are not limited to two cases; they can have tens of cases.
- Monkey Game Development:Beginner's Guide
- Vue.js 3.0源碼解析(微課視頻版)
- 自然語言處理Python進階
- 軟件品質之完美管理:實戰經典
- 開源項目成功之道
- R語言數據可視化:科技圖表繪制
- Learning Nessus for Penetration Testing
- iOS開發項目化入門教程
- HTML5移動前端開發基礎與實戰(微課版)
- Python機器學習與量化投資
- Oracle Database XE 11gR2 Jump Start Guide
- VMware vSphere 5.5 Cookbook
- 算法精解:C語言描述
- Python趣味創意編程
- Learning jqPlot