Branch
Hash :
6244fe3b
Author :
Thomas de Grivel
Date :
2025-10-21T18:32:49
Make doc
KC3 structs are a key value associative data structure with default values and arbitrary property order.
They are compatible with C structs of the same type.
ikc3> a = %KC3.Op{sym: :dot,
precedence: 10,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
You can define one struct type per module with defstruct for
example in vec2d.kc3 :
defmodule Vec2D do
defstruct [x: 0.0,
y: 0.0]
end
Destructuring works with structs to extract values :
ikc3> %KC3.Op{sym: sym} = ^ a
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
ikc3> sym
:dot
You can use the dot syntax to access struct values from a Sym key :
ikc3> a = %KC3.Op{sym: :dot,
precedence: 10,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
ikc3> a.sym
:dot
ikc3> a.arity
2
ikc3> a.callable
fn (a, b) { a.x * b.y + a.y * b.y }
The bracket syntax allows you to query structs the same as maps :
ikc3> a[:sym]
:dot
ikc3> a[:arity]
2
ikc3> a[:callable]
fn (a, b) { a.x * b.y + a.y * b.y }
You can also use the KC3.access function for the same result :
ikc3> a = %KC3.Op{sym: :dot,
precedence: 10,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
ikc3> access(a, [:sym])
:dot
ikc3> access(a, [:arity])
2
ikc3> access(a, [:callable])
fn (a, b) { a.x * b.y + a.y * b.y }
To update an existing struct property you can use Struct.put, a new struct with modified properties will be returned. E.g. :
ikc3> a = %KC3.Op{sym: :dot,
precedence: 10,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
b = Struct.put(a, :sym, :·)
%KC3.Op{sym: :·,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
Top : KC3 documentation
Previous : 1.20 Str
Next : 1.22 Sym
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
# 1.21 Struct
KC3 structs are a key value associative data structure with default
values and arbitrary property order.
They are compatible with C structs of the same type.
```elixir
ikc3> a = %KC3.Op{sym: :dot,
precedence: 10,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
```
You can define one _struct type_ per module with `defstruct` for
example in `vec2d.kc3` :
```elixir
defmodule Vec2D do
defstruct [x: 0.0,
y: 0.0]
end
```
Destructuring works with structs to extract values :
```elixir
ikc3> %KC3.Op{sym: sym} = ^ a
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
ikc3> sym
:dot
```
You can use the dot syntax to access struct values from a `Sym` key :
```elixir
ikc3> a = %KC3.Op{sym: :dot,
precedence: 10,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
ikc3> a.sym
:dot
ikc3> a.arity
2
ikc3> a.callable
fn (a, b) { a.x * b.y + a.y * b.y }
```
The bracket syntax allows you to query structs the same as maps :
```elixir
ikc3> a[:sym]
:dot
ikc3> a[:arity]
2
ikc3> a[:callable]
fn (a, b) { a.x * b.y + a.y * b.y }
```
You can also use the `KC3.access` function for the same result :
```elixir
ikc3> a = %KC3.Op{sym: :dot,
precedence: 10,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
ikc3> access(a, [:sym])
:dot
ikc3> access(a, [:arity])
2
ikc3> access(a, [:callable])
fn (a, b) { a.x * b.y + a.y * b.y }
```
To update an existing struct property you can use Struct.put, a new
struct with modified properties will be returned. E.g. :
```elixir
ikc3> a = %KC3.Op{sym: :dot,
precedence: 10,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
%KC3.Op{sym: :dot,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
b = Struct.put(a, :sym, :·)
%KC3.Op{sym: :·,
arity: 2,
special: false,
precedence: 10,
associativity: 1,
callable: fn (a, b) { a.x * b.y + a.y * b.y }}
```
---
Top : [KC3 documentation](../)
Previous : [1.20 Str](1.20_Str)
Next : [1.22 Sym](1.22_Sym)