doc: sum types
parent
7e71799980
commit
4de48e86d2
46
doc/docs.md
46
doc/docs.md
|
@ -4,14 +4,18 @@
|
||||||
|
|
||||||
V is a statically typed compiled programming language designed for building maintainable software.
|
V is a statically typed compiled programming language designed for building maintainable software.
|
||||||
|
|
||||||
It's similar to Go and is also influenced by Oberon, Rust, Swift.
|
It's similar to Go and is also influenced by Oberon, Rust, Swift, Python.
|
||||||
|
|
||||||
V is a very simple language. Going through this documentation will take you about half an hour,
|
V is a very simple language. Going through this documentation will take you about half an hour,
|
||||||
and by the end of it you will learn pretty much the entire language.
|
and by the end of it you will learn pretty much the entire language.
|
||||||
|
|
||||||
|
The language promotes writing simple and clear code with a minimal amount of abstractions.
|
||||||
|
|
||||||
Despite being simple, it gives a lot of power to the developer. Anything you can do in other languages,
|
Despite being simple, it gives a lot of power to the developer. Anything you can do in other languages,
|
||||||
you can do in V.
|
you can do in V.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Hello World
|
## Hello World
|
||||||
|
|
||||||
```v
|
```v
|
||||||
|
@ -945,6 +949,46 @@ color = .green
|
||||||
println(color) // "1" TODO: print "green"?
|
println(color) // "1" TODO: print "green"?
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Sum types
|
||||||
|
|
||||||
|
```v
|
||||||
|
type Expr = BinaryExpr | UnaryExpr | IfExpr
|
||||||
|
|
||||||
|
struct BinaryExpr{ ... }
|
||||||
|
struct UnaryExpr{ ... }
|
||||||
|
struct IfExpr{ ... }
|
||||||
|
|
||||||
|
struct CallExpr {
|
||||||
|
args []Expr
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (p mut Parser) expr(precedence int) ast.Expr {
|
||||||
|
match p.tok {
|
||||||
|
.key_if { return IfExpr{} }
|
||||||
|
...
|
||||||
|
else { return BinaryExpr{} }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen(expr Expr) {
|
||||||
|
match expr {
|
||||||
|
.key_if { gen_if(it) }
|
||||||
|
...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_if(expr IfExpr) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
To check whether a sum type is a certain type, use `is`:
|
||||||
|
|
||||||
|
```v
|
||||||
|
println(expr is IfExpr)
|
||||||
|
```
|
||||||
|
|
||||||
## Option/Result types & error handling
|
## Option/Result types & error handling
|
||||||
|
|
||||||
```v
|
```v
|
||||||
|
|
Loading…
Reference in New Issue