| 1 | import veb |
| 2 | |
| 3 | const embedded_file = $embed_file(r'C:\Users\user\path\to\file') |
| 4 | |
| 5 | struct App { |
| 6 | a string |
| 7 | b string |
| 8 | mut: |
| 9 | c int |
| 10 | d f32 |
| 11 | pub: |
| 12 | e f32 |
| 13 | f u64 |
| 14 | pub mut: |
| 15 | g string |
| 16 | h u8 |
| 17 | } |
| 18 | |
| 19 | fn comptime_for() { |
| 20 | println(@FN) |
| 21 | $for method in App.methods { |
| 22 | println(' method: ${method.name} | ${method}') |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn comptime_for_with_if() { |
| 27 | println(@FN) |
| 28 | $for method in App.methods { |
| 29 | println(' method: ${method}') |
| 30 | $if method.typ is fn () { |
| 31 | assert method.name in ['run', 'method2'] |
| 32 | } |
| 33 | $if method.return_type is int { |
| 34 | assert method.name in ['int_method1', 'int_method2'] |
| 35 | } |
| 36 | $if method.args[0].typ is string { |
| 37 | assert method.name == 'my_method' |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn comptime_for_fields() { |
| 43 | println(@FN) |
| 44 | $for field in App.fields { |
| 45 | println(' field: ${field.name} | ${field}') |
| 46 | $if field.typ is string { |
| 47 | assert field.name in ['a', 'b', 'g'] |
| 48 | } |
| 49 | $if field.typ is f32 { |
| 50 | assert field.name in ['d', 'e'] |
| 51 | } |
| 52 | if field.is_mut { |
| 53 | assert field.name in ['c', 'd', 'g', 'h'] |
| 54 | } |
| 55 | if field.is_pub { |
| 56 | assert field.name in ['e', 'f', 'g', 'h'] |
| 57 | } |
| 58 | if field.is_pub && field.is_mut { |
| 59 | assert field.name in ['g', 'h'] |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | struct Result {} |
| 65 | |
| 66 | fn (mut a App) my_method(p string) Result { |
| 67 | println('>>>> ${@FN} | p: ${p}') |
| 68 | return Result{} |
| 69 | } |
| 70 | |
| 71 | fn handle_conn[T](mut app T) { |
| 72 | $for method in T.methods { |
| 73 | $if method.return_type is Result { |
| 74 | app.$method('abc', 'def') |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | fn comptime_call_dollar_method() { |
| 80 | mut app := App{} |
| 81 | handle_conn[App](mut app) |
| 82 | } |
| 83 | |
| 84 | fn (mut app App) create() veb.Result { |
| 85 | return $veb.html() |
| 86 | } |
| 87 | |
| 88 | fn main() { |
| 89 | comptime_for() |
| 90 | comptime_for_with_if() |
| 91 | comptime_for_fields() |
| 92 | comptime_call_dollar_method() |
| 93 | } |
| 94 | |