You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+35-9Lines changed: 35 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,8 @@ Mostly quality-of-life improvements, especially when handling arrays.
49
49
50
50
## Limitations
51
51
52
+
* Avoid using global scope for Julia function call. Always have everything inse proc / func. It's good practice anyway
53
+
52
54
* Value conversion Nim -> Julia are done **by copy**.
53
55
* Arrays are an exception to this rule and can be created from buffer / are accessible using a buffer.
54
56
@@ -63,24 +65,33 @@ Mostly quality-of-life improvements, especially when handling arrays.
63
65
* If you need Windows support, consider opening an issue or a PR :).
64
66
* Otherwise, just use WSL
65
67
66
-
# Examples
68
+
# Examples & tips
69
+
70
+
## Examples
67
71
68
72
Here is the basic example:
69
73
```nim
70
74
import nimjl
75
+
proc main() =
76
+
Julia.init() # Initialize Julia VM. Subsequent call will be ignored
77
+
78
+
var myval = 4.0'f64
79
+
# Call Julia function "sqrt" and convert the result to a float
80
+
var res = Julia.sqrt(myval).to(float64)
81
+
echo res # 2.0
71
82
72
-
Julia.init() # Initialize Julia VM. Subsequent call will be ignored
73
-
74
-
var myval = 4.0'f64
75
-
# Call Julia function "sqrt" and convert the result to a float
76
-
var res = Julia.sqrt(myval).to(float64)
77
-
echo res # 2.0
83
+
when isMainModule:
84
+
main()
78
85
79
86
```
80
87
88
+
JlVmExit() seems optionnal. It's present in the C API but not calling it doesn't seem to cause any problem.
89
+
90
+
Nonetheless, if you use OS resources from Julia it is probably better to call Julia.exit() / JlVmExit() for a clean exit.
91
+
81
92
## Setting up Julia dependency
82
93
83
-
It is now possible to embed Julia files inside a Nim compiled binary to easily distribute Julia code. To make distribution possible, an API to call ``Pkg.add("...")`` has also been added **with version number easy to specify**.
94
+
*It is now possible to embed Julia files inside a Nim compiled binary to easily distribute Julia code. To make distribution possible, an API to call ``Pkg.add("...")`` has also been added **with version number easy to specify**.
84
95
85
96
```nim
86
97
import nimjl
@@ -100,10 +111,25 @@ Julia.init:
100
111
# embed specific file; path should be relative to ``getProjectPath()``
101
112
file("localfile.jl")
102
113
```
103
-
See examples/ex09_embed_file.nim for a concrete example
114
+
115
+
Note that the order of the file matters.
116
+
See examples/ex09_embed_file.nim for a concrete example.
104
117
105
118
Take a look at ``tests/`` or ``examples/`` folder for typical examples.
106
119
120
+
* You can use Pkg: activate() to setup a virtual env
121
+
* Alternatively, you can embed a Julia file that setup your environment and dependencies and embed it **first**.
122
+
* Because files are evaluated in the order they are embedded, it will deterine the env for all the other files.
123
+
124
+
## Debugging
125
+
126
+
* Most error will come from incorrect type passed between Julia and Nim. Check function interface and return type first.
127
+
128
+
* If you have random segfault that are non-reproductible, that may be a cause of the Julia GC cleaning memory that Nim uses. Consider using jlGcRoot.
129
+
130
+
* If you do not work with fixed version package for Julia, you are at risk of code breaking when packages are updated / upgraded.
0 commit comments