nim-lang

Iterator-producing function in Nim: works when assigning the iterator, stuck when calling it directly

寵の児 提交于 2021-01-27 12:12:59
问题 I tried to make a procedure that creates an iterator, as follows: proc makeCDFrom(start: int): iterator(): int = result = iterator(): int = var i: int = start while i >= 0: echo "i:", i yield(i) dec(i) let cdFrom6 = makeCDFrom(6) for j in cdFrom6(): echo "j:", j This appears to work as expected: i:6 j:6 i:5 j:5 i:4 j:4 i:3 j:3 i:2 j:2 i:1 j:1 i:0 j:0 However, initially, I had tried with this slight variation: proc makeCDFrom(start: int): iterator(): int = result = iterator(): int = var i: int

How do you do string interpolation with variables in nim?

谁说我不能喝 提交于 2020-08-20 11:18:22
问题 I've seen that you can interpolate strings with fmt like so: let msg = "hello" echo fmt"{msg}\n" But in my case, the interpolated string is quite long. I would prefer to assign said text to a variable and then the the interpolation later, like so: let msg = "..... long text here {place_holder1}...." echo interpolate(msg, var1, etc) Is this possible? 回答1: Yes, see strutils.format. strutils also comes with the % operator which can be used like: let str = "$#, $#, $#" let interp = str % ["One",

What is a simple way to play a .wav file in Nim on OSX?

冷暖自知 提交于 2020-01-24 19:16:20
问题 I am trying to play a wav file in a very simple program that looks like this, currently attempting to use nim-csfml: import csfml_audio var alarmsong = newMusic("alarm.wav") alarmsong.play() but it appears to be relying on the existence of libcsfml.audi o, and while my program compiles just fine, when I try to actually run it I get an error | => ./alarm could not load: libcsfml-audio.so (I have a libcsfml-audio.dylib instead, being that I used the OSX shared libraries for csfml/sfml ) Is

typed vs untyped vs expr vs stmt in templates and macros

﹥>﹥吖頭↗ 提交于 2019-12-29 04:49:12
问题 I've been lately using templates and macros, but i have to say i have barely found information about these important types. This is my superficial understanding: typed/expr is something that must exists previously, but you can use .immediate. to overcome them. untyped/stmt is something that doesn't to be defined previously/one or more statements. This is a very vague notion of the types. I'd like to have a better explanation of them, including which types should be used as return. 回答1: The

How to correctly create a nim/nimrod windows dll

时光怂恿深爱的人放手 提交于 2019-12-23 10:59:05
问题 I want to create a dll from nim code. But i failed to register some other exports than "NimMainInner". Even if i try this simple example its not working: proc Hellow(): cint {.exportc.} = echo("hello") return 1 i've compiled it with nim c --app:lib libh4x.nim and nim c -d:release --app:lib --no_main libh4x.nim i use Nim Compiler Version 0.11.2 (2015-05-04) [Windows: i386] to inspect the dll i use dllexp.exe . I've also tried to load the dll with python ctypes, but none of my exports are shown

Nim: Addresses of parameters and mutability

♀尐吖头ヾ 提交于 2019-12-18 07:40:31
问题 I'm trying to make up my mind about Nim's policy behind expression has no address . In particular, I have a C function which takes a pointer (+ length etc.) of some data buffer. I know that this function will not modify the data. Simplified: type Buffer = object data: seq[float] proc wrapperForCCall(buf: Buffer) = # accessing either buf.addr nor buf.data.addr produces # Error: expression has no address # workaround: var tmp = buf.data # costly copy callToC(tmp.len, tmp.addr) # now it works On

default arguments to template's expressions

隐身守侯 提交于 2019-12-11 14:58:37
问题 If I want to make a template that can accept 2 untyped arguments, and pass them through do notation, when I omit the second do I'd like to have a way to specify a fallback in the form of a parameter's default value. as such: template tpl(x: bool, body: untyped, bodyFinally: untyped): void = if x: body else: bodyFinally #call site: var r: int tpl(true) do: r = 2 do: raise newException(Exception, "") This works, but that: template tpl(x: bool, body: untyped, bodyFinally: untyped = discard):

Run Nim code at compile time

点点圈 提交于 2019-12-10 17:08:46
问题 So I know that if I define a const Nim will evaluate whatever I assign to it at compile time, so I could do something like this: proc compileTimeCode: bool = # Put code here return true const _ = compileTimeCode() and then I could put my code in the compileTimeCode proc. This works, but seems messy, overcomplicated and unintuitive. It also requires more typing than it should, and is difficult to DRY up. 回答1: What's the question? If there is a better way to run code at compile time? static: #