|
7 | 7 | y: float |
8 | 8 | z: string |
9 | 9 |
|
10 | | -Julia.init() # Initialize Julia VM. This should be done once in the lifetime of your program. |
11 | | -# Include Julia file |
12 | | -jlInclude("localmodule.jl") |
13 | | -# Use the module. If you're confused by the syntax, go and read through Julia's Manual where module usage is explained |
14 | | -jlUseModule(".nimjlExample") |
| 10 | +proc main() = |
| 11 | + Julia.init() # Initialize Julia VM. This should be done once in the lifetime of your program. |
| 12 | + # Include Julia file |
| 13 | + jlInclude("localmodule.jl") |
| 14 | + # Use the module. If you're confused by the syntax, go and read through Julia's Manual where module usage is explained |
| 15 | + jlUseModule(".nimjlExample") |
15 | 16 |
|
16 | | -block: # Tuple handling |
17 | | - # Look, you can pass Nim tuple to Julia |
18 | | - var mytup: MyTuple = (nimTupKey1: 1, nimTupKey2: 2) |
19 | | - # Convert Nim tuple to Julia tuple automatically |
20 | | - var res = Julia.customFunction(mytup) |
21 | | - # Convert Julia tuple to Nim tuple |
22 | | - var nimres = res.to(MyTuple) |
| 17 | + block: # Tuple handling |
| 18 | + # Look, you can pass Nim tuple to Julia |
| 19 | + var mytup: MyTuple = (nimTupKey1: 1, nimTupKey2: 2) |
| 20 | + # Convert Nim tuple to Julia tuple automatically |
| 21 | + var res = Julia.customFunction(mytup) |
| 22 | + # Convert Julia tuple to Nim tuple |
| 23 | + var nimres = res.to(MyTuple) |
23 | 24 |
|
24 | | - echo myTup |
25 | | - echo nimres |
| 25 | + echo myTup |
| 26 | + echo nimres |
26 | 27 |
|
27 | | - doAssert myTup.nimTupKey1+1 == nimres.nimTupKey1 |
28 | | - doAssert myTup.nimTupKey2+1 == nimres.nimTupKey2 |
| 28 | + doAssert myTup.nimTupKey1+1 == nimres.nimTupKey1 |
| 29 | + doAssert myTup.nimTupKey2+1 == nimres.nimTupKey2 |
29 | 30 |
|
30 | | -block: # Object manipulation |
31 | | - # Call constructor |
32 | | - var foo = Julia.makeFoo() |
33 | | - # Access fields with dot syntax |
34 | | - # Calls getproperty in Julia side |
35 | | - echo foo.x |
36 | | - echo foo.y |
37 | | - echo foo.z |
38 | | - # Modify fields with .= syntax |
39 | | - # Calls setproperty! on Julia side |
40 | | - foo.x = 2 |
41 | | - foo.y = 3.14 |
42 | | - foo.z = "General Kenobi !" |
43 | | - # Yay this has been modified |
44 | | - echo foo |
| 31 | + block: # Object manipulation |
| 32 | + # Call constructor |
| 33 | + var foo = Julia.makeFoo() |
| 34 | + # Access fields with dot syntax |
| 35 | + # Calls getproperty in Julia side |
| 36 | + echo foo.x |
| 37 | + echo foo.y |
| 38 | + echo foo.z |
| 39 | + # Modify fields with .= syntax |
| 40 | + # Calls setproperty! on Julia side |
| 41 | + foo.x = 2 |
| 42 | + foo.y = 3.14 |
| 43 | + foo.z = "General Kenobi !" |
| 44 | + # Yay this has been modified |
| 45 | + echo foo |
45 | 46 |
|
46 | | - # You can use dot syntax on Julia value to call proc as well |
47 | | - discard foo.applyToFoo() |
48 | | - echo foo |
| 47 | + # You can use dot syntax on Julia value to call proc as well |
| 48 | + discard foo.applyToFoo() |
| 49 | + echo foo |
49 | 50 |
|
50 | | -block: |
51 | | - var foo = Foo(x: 12, y: 15, z: "This string comes from Nim") |
52 | | - # You can convert Nim to Julia object if : |
53 | | - # * fields have the same name and type (fieldName becomdes Julia symbol) |
54 | | - # * The Julia type have an empty constructor -- Nim needs to initialize the Julia variable before calling setproperty! providing a default empty constructor is the easiest way of doing it |
55 | | - var jlfoo = toJlVal(foo) |
56 | | - echo jlfoo |
57 | | - echo jltypeof(jlfoo) # This echo "Foo" -> Julia sees this as a Foo mutable struct type |
| 51 | + block: |
| 52 | + var foo = Foo(x: 12, y: 15, z: "This string comes from Nim") |
| 53 | + # You can convert Nim to Julia object if : |
| 54 | + # * fields have the same name and type (fieldName becomdes Julia symbol) |
| 55 | + # * The Julia type have an empty constructor -- Nim needs to initialize the Julia variable before calling setproperty! providing a default empty constructor is the easiest way of doing it |
| 56 | + var jlfoo = toJlVal(foo) |
| 57 | + echo jlfoo |
| 58 | + echo jltypeof(jlfoo) # This echo "Foo" -> Julia sees this as a Foo mutable struct type |
58 | 59 |
|
59 | | - discard jlfoo.applyToFoo() |
60 | | - # Object are copid during conversions so modifying jlfoo does not modify foo |
61 | | - # There is an exception to this for Array fields -- see ex_arrays for explanation |
62 | | - echo jlfoo |
63 | | - echo foo |
| 60 | + discard jlfoo.applyToFoo() |
| 61 | + # Object are copid during conversions so modifying jlfoo does not modify foo |
| 62 | + # There is an exception to this for Array fields -- see ex_arrays for explanation |
| 63 | + echo jlfoo |
| 64 | + echo foo |
64 | 65 |
|
65 | | -block: |
66 | | - let res = Julia.returnDoubleValue() |
67 | | - echo res.myint |
68 | | - echo res.mystr |
| 66 | + block: |
| 67 | + let res = Julia.returnDoubleValue() |
| 68 | + echo res.myint |
| 69 | + echo res.mystr |
69 | 70 |
|
| 71 | +when isMainModule: |
| 72 | + main() |
0 commit comments