Docs: LanguageGuide: defdynamic, match form, algebraic types, pattern literal fix

This commit is contained in:
GrayJack 2019-11-03 21:38:47 -03:00
parent 16e9418d25
commit adc131787f

View File

@ -25,7 +25,7 @@ To learn more about the details of memory management, check out [Memory.md](http
10.0 ;; Double
true ;; Bool
"hello" ;; &String
\#"hello" ;; &Pattern
#"hello" ;; &Pattern
\e ;; Char
[1 2 3] ;; (Array Int)
{1 1.0 2 2.0} ;; (Map Int Double)
@ -61,6 +61,7 @@ foo ; symbol
(definterface interface-name (Fn [<t1> <t2>] <return>)) ;; Define a generic function that can have multiple implementations
(def variable-name value) ;; Define a global variable (only handles primitive constants for the moment)
(defmacro <name> [<arg1> <arg2> ...] <macro-body>) ;; Define a macro, its argument will not be evaluated when called
(defdynamic <name> <value>) ;; A variable that can only be used at the REPL or during compilation
(defndynamic <name> [<arg1> <arg2> ...] <function-body>) ;; A function that can only be used at the REPL or during compilation
(defmodule <name> <definition1> <definition2> ...) ;; The main way to organize your program into smaller parts
```
@ -75,6 +76,7 @@ and other static analysis. The first three of them are also available in dynamic
(do <expr1> <expr2> ... <return-expression>) ;; Perform side-effecting functions, then return a value
(if <expression> <true-branch> <false-branch>) ;; Branching
(while <expression> <body>) ;; Loop until expression is false
(match <expression> <case1> <expression1> <case2> <expression2> ...) ;; Pattern match the first expression in a exhaustive cases
(ref <expression>) ;; Borrow an owned value
(address <expression>) ;; Takes the memory address of a value, returns a C-style pointer
(set! <variable> <expression>) ;; Mutate a variable
@ -172,17 +174,17 @@ When you `use` a module, its declarations are brought into the current scope. If
```clojure
(use String)
;; Only the `String` module is used in the global scope,
;; Only the `String` module is used in the global scope,
;; so we can refer to `length` without a module qualifier.
(defn f [x]
(length x))
(defmodule Foo
(use Array)
;; Since the the `String` module is used in the global scope,
;; and the Foo module `use`s `Array`, we again need to qualify calls to `length`
;; Since the the `String` module is used in the global scope,
;; and the Foo module `use`s `Array`, we again need to qualify calls to `length`
;; to disambiguite which declaration we're referring to.
(defn g [xs]
(defn g [xs]
(Array.length xs)))
```
@ -190,15 +192,15 @@ Sometimes, it's more convenient to bring a module's declarations into scope only
```clojure
(defmodule Foo
;; we need to use a module qualifier here,
;; we need to use a module qualifier here,
;; since there's no call to `use` in the `Foo` module scope.
(defn f [x]
(defn f [x]
(String.length x))
;; Using the `with` form, we can reference the module's declarations
;; Using the `with` form, we can reference the module's declarations
;; unqualified in all the forms contained in the `with`'s scope.
(with String
(defn g [x]
(defn g [x]
(length x)))
)
```
@ -217,6 +219,23 @@ Sometimes, it's more convenient to bring a module's declarations into scope only
(Vector2.update-x my-pos inc) ;; => (Vector2 11 20)
```
### Enums / Algebraic types
```clojure
(deftype (Either a b)
(Left a)
(Right b))
;; A Variant can be created with the same syntax as call expression
(Left 10)
(Right 11)
;; You can use pattern matching to extract values in a safe way
(defn get [either]
(match either
(Left a) a
(Right b) b))
```
### C Interop
```clojure
(system-include "math.h") ;; compiles to #include <math.h>