Re: How do I make a table of tables in Nim?
I really think you should use objects for this, but this will work anyway: import tables var books = newTable[string, TableRef[string, string]]() books["one"] = newTable[string, string]() books["one"]["price"] = "150" echo books["one"]["price"] Run
Re: Xors3D game engine for Nim [wrapper]
What about Godot? It also has Nim bindings and Godot generally has a pretty good 3D support already.
Simple snippet for colored logging with named arguments
I just wanted to have logging which has colors, and also have chronicles-style printing of arguments, so I created this relatively small snippet for logging: [https://gist.github.com/Yardanico/0bcf50c05b71ae8990529ea0913ac068](https://gist.github.com/Yardanico/0bcf50c05b71ae8990529ea0913ac068) Example of usage: import simple_log let info = 5 logInfo("Hello world!", info = info, count = 65, word = "world") Run I don't really think I'll release it as a package because it's just too small, but you can easily embed it in your projects.
Simple snippet for easy colored logging with levels
I just wanted to have logging which has colors, and also have chronicles-style printing of arguments, so I created this relatively small snippet for logging: [https://gist.github.com/Yardanico/5e2c012b184d45c4a845f820927cc2e1](https://gist.github.com/Yardanico/5e2c012b184d45c4a845f820927cc2e1) I don't really think I'll release it as a package because it's just too small, but you can easily embed it in your projects.
Re: Is there a handy way to declare Rune literals?
Since Unicode contains all ASCII characters you can easily do that: import unicode let my_rune = Rune(ord('.')) echo my_rune Run
Re: Question About Nim 32/64-bits
Multilib IS supported, you need to compile your file like nim c --cpu:i386 --passL:"-m32" \--passC:"-m32" myfile.nim
Re: Nim versus Julia benchmark comparison
@mratsim Why does Nim need warm-up? I've never heard about that.
Re: cannot import windows
You should do "import oldwinapi/windows", not "import windows".
Re: question on range types
They are checked at runtime, but not in release mode (because -d:release does --rangeChecks:off)
Re: How do you get thread count in Nim
TS probably means "processor threads" \- Hyper Threading
Re: Nim Manual: wrong link: "lock levels"
Please report it on github!
Re: request for feedback - type safe OpenGL wrapper
jackmott: I really think you should use templates instead of inlined procs here, because it's not guaranteed that proc will be inlined (even with inline pragma)
Nim discord server
I've created a new Discord Nim channel: [https://discord.gg/ezDFDw2](https://discord.gg/ezDFDw2)
Re: What should next Araq's live stream be about?
The recording is now available! [https://www.youtube.com/watch?v=KNUDGZuqfQM](https://www.youtube.com/watch?v=KNUDGZuqfQM)
What should d0m96 work on in his next Nim livestream?
The livestream starts at 4PM (16:00) UTC 20 Oct 2017 [http://www.strawpoll.me/14185591](http://www.strawpoll.me/14185591)
Re: nim-cookbook
You don't need to declare a tuple type here: [http://nim-cookbook.btbytes.com/dsalgo.html](http://nim-cookbook.btbytes.com/dsalgo.html) It can be done with: let address = ("Royal Rd", "Kingville", 11234) let (s,c,z) = address echo s echo c echo z
What should my next Araq's stream be about?
[http://www.strawpoll.me/14163114](http://www.strawpoll.me/14163114) Vote for your favourite topic! Araq will be streaming on Thursday, 19:00 UTC!
Re: "Person = object" still has a type field by default?
Yes, not using object of meant you were "pure"
Re: noob confusion stream of conciousness
About toString - in Nim $ is used as a stringify operator for almost all types anywhere (not only in stdlib). So firstly try $ on a type
Re: Ideas for calling CGAL from Nim?
c2nim already supports C++ templates
Re: Python-like with, context managers, and the RAII pattern
cdunn2001: "There's actually a lot of places I'd really like to use "withAs" in, eg to make sure that resources are closed, but without needing to add a resource-specific "defer" line." So wizzardx will need to create a template like this for every type he'll use
Re: "Cross-platform C" target for Nim?
wizzardx: yes, and nim uses niminst
Re: Simple logging with module filename and line number
coffeepot: I'm afraid it isn't possible to implement it using a variable (e.g. something like $line). Yeah, it is possible to add this just as template, but I don't really think it would be accepted in stdlib
Simple logging with module filename and line number
Nim allows getting a line number from a template (via compile-time information). So you can make a log template to do that: import logging, strutils var logger = newConsoleLogger(fmtStr = "[$time][$levelid]") addHandler(logger) template log*(lvl: Level, data: string): untyped = let pos {.compiletime.} = instantiationInfo() const addition = when defined(release): "[$1] " % [pos.filename] else: "[$1:$2] " % [pos.filename, $pos.line] logger.log(lvl, addition & data) template log*(data: string): untyped = log(lvlInfo, data) log("hi world") If compiled in non-release mode, it will contain line number & filename. If compiled in release mode - only filename, but this can be easily changed.
Re: How to
monster: you wouldn't get only "syntax highlighting" with VSCode. Nim plugin also supports "nim check" \- checking files for errors, and nimsuggest - code completion (it knows everything about Nim)
Re: directory structure for a multi-file project?
[https://gist.github.com/stisa/09474a952a420448778685507d3fbd51](https://gist.github.com/stisa/09474a952a420448778685507d3fbd51)
Re: Python-like with, context managers, and the RAII pattern
well, no, only "with" and "without": [https://github.com/nim-lang/Nim/commit/3ccc9c467d84dc8c3412acbea20fc10b5335eaa8](https://github.com/nim-lang/Nim/commit/3ccc9c467d84dc8c3412acbea20fc10b5335eaa8)
Re: Python-like with, context managers, and the RAII pattern
Udiknedormin: you can change "take" to "with", since "with" and "without" were removed from Nim keyword list in devel
Re: Python-like with, context managers, and the RAII pattern
It's very easy with "quote do": import macros proc my_enter(x: int): int = echo "my_enter was called" return x proc my_exit(x: int) = echo "my_exit was called" macro take(args, body: untyped): untyped = # Value of an argument let varValue = args[1] # Variable name let varName = args[^1] result = quote do: # Private variable would be "private", so it wouldn't be redefined var private = `varValue` # We need to have a block to make sure varName is not redefined block: var `varName` = my_enter(private) try: `body` finally: my_exit(private) take 3 as f: echo f take 6 as f: echo f
Re: Python-like with, context managers, and the RAII pattern
You can use defer for closing resources: [https://nim-lang.org/docs/manual.html#exception-handling-defer-statement](https://nim-lang.org/docs/manual.html#exception-handling-defer-statement)
Re: Are array types that differ only in index boundaries different?
LeuGim: a side note: it's not an implicit conversion, with "var x: int8" Nim treats your "3" literal as "int8" type
Re: Error: cannot instantiate: 'OrderedTable'
sflennik: your code is wrong, OrderedTable can't contain different types It should be like this: from tables import OrderedTable, toOrderedTable proc getMetaInfo(filename: string, fileSize: int64): OrderedTable[string, string] = result = { "filename": filename, "size": $fileSize, }.toOrderedTable var testing = getMetaInfo("filename", 44)
mathexpr, a math expression evaluator library in Nim
[https://github.com/Yardanico/nim-mathexpr](https://github.com/Yardanico/nim-mathexpr) This is a mathematic expression evaluator library in pure Nim (with no third-party dependencies). Basically it is a recursive descent evaluator. It supports many mathematical functions, also you can provide variables and add custom functions. Also it doesn't have strict rules on arguments, so all of these are valid: sqrt(max(1, 2)) sqrt(max(1 2)) sqrt max(1, 2) sqrt max(1 2) # You can even do something like this! sqrt fac log2 10 Example REPL: ## An example REPL for Mathexpr: import strutils, rdstdin, ./mathexpr, tables # Our variables (they will be available in the REPL) var ourVars = {"x": 5.0, "y": 6.0, "z": 75.0}.newTable() # Procedure should have this type: # proc(args: seq[float]): float proc mySum(args: seq[float]): float = for arg in args: result += arg # Add our custom `sum` function mathexpr.functions["sum"] = mySum while true: var expr: string try: expr = readLineFromStdin("> ") except IOError: echo "Goodbye!" quit() if expr in ["exit", "quit", "quit()", "exit()"]: quit(0) try: let result = eval(expr, ourVars) echo "$1 = $2" % [expr, $result] except: echo getCurrentExceptionMsg() continue By the way, implementation is less than 200 lines and it works in JS backend
Re: Using Plotly.js in Nim
Well you would wrap this functions too? And I really think you should make it a real wrapper
Re: Nimscript: setLen segfaults?
Udiknedormin: can you post a code example? And also create an issue on github
Re: Too many global variables in a generated js-code
@alexsad - don't try to compare JS code compiled by Nim with hand-written JS. It's almost the same if you compare hand-written C++ with C++ code compiled by Nim
Re: Containers of generic types
cdome: but one problem - vtrefs are not implemented yet
Re: Glob patterns for nim
[https://nim-lang.org/docs/os.html#walkPattern](https://nim-lang.org/docs/os.html#walkPattern) ?
Re: Random idea - porting python 3 stdlib to Nim.
[https://github.com/Yardanico/nimpylib](https://github.com/Yardanico/nimpylib)
Re: Differences: Object variant vs static conditional fields
Yes, Stefan_Salewski is right: type NodeKind = enum # the different node types nkInt, # a leaf with an integer value nkFloat# a leaf with a float value # Version 1 with object variant Node = ref NodeObj NodeObj = object case kind: NodeKind # the ``kind`` field is the discriminator of nkInt: intVal: int of nkFloat: floatVal: float # Version 2 with conditional fields Node2[NK] = ref NodeObj2[NK] NodeObj2[NK: static[NodeKind]] = object when NK == nkInt: intVal: int elif NK == nkFloat: floatVal: float # Compiles fine let a = @[NodeObj(kind: nkInt), NodeObj(kind: nkFloat)] # Doesn't compile let b = @[NodeObj2[nkInt](), NodeObj2[nkFloat]()]
Re: Killing an AsyncHttpServer
Try to add gcsafe pragma to your proc
Re: Compositional inheritance voodo
You can also try concepts
Re: Nim newbie request/challenge
But you shouldn't write export PATH... in terminal you should write it in your .bashrc or .zshrc or .rc
Re: Convert tuple into a Object
My macro: import macros macro createObj(typ: typedesc, data: untyped): untyped = # This macro only accepts tuples in parenthesis, but this can be changed assert data.kind == nnkPar # Object construction syntax result = newTree(nnkObjConstr) # Add type name result.add(typ.getTypeInst()[1]) # Get implementation of typedesc let typImpl = typ.getTypeInst()[1].symbol.getImpl()[2] # We support both ref and usual objects let fields = if typImpl.kind == nnkRefTy: typImpl[0][2] else: typImpl[2] # Tuple should have exactly the same number of values as object assert fields.len == data.len # add fieldName: value to result for all fields/values for i in 0..
Re: Convert tuple into a Object
You can definitely do this with a macro
Re: Nim newbie request/challenge
Maybe you can't access Nim directory from user account because it has wrong permissions? And also maybe you have another Nim installed?
Re: Simple Python-like class macro and some python-like procedures (print, input, range, others)
Udiknedormin: yeah, or maybe IRC
Re: Simple Python-like class macro and some python-like procedures (print, input, range, others)
Udiknedormin, I've added lazy evaluation for range - [https://github.com/Yardanico/nimpylib/blob/master/src/pylib/range.nim](https://github.com/Yardanico/nimpylib/blob/master/src/pylib/range.nim)
Re: Simple Python-like class macro and some python-like procedures (print, input, range, others)
Udiknedormin: I've fixed modulo (If I'm not mistaken), added a couple of tests. About range - I plan to make it an object, so it would be lazy too (as in python 3) I can't write print as an simple procedure because Nim doesn't have keyword-only arguments
Re: Reason for -fno-strict-aliasing?
I've even changed -fno-strict-aliasing to "-fstrict-aliasing -Wstrict-aliasing" in build.sh (in csources), and nim compiler compiled without any issues!
Re: Reason for -fno-strict-aliasing?
@cblake it doesn't show any problems for me
Simple Python-like class macro and some python-like procedures (print, input, range, others)
I've implemented some simplest Python functions and very simple "class" macro (which doesn't and wouldn't support inheritance). [https://github.com/Yardanico/nimpylib](https://github.com/Yardanico/nimpylib) \- check example2.nim for classes. Yeah, code isn't the best - this is mainly due to the nature of Python (dynamic-typing), for example: "range(5)" \- here 5 is a position where range stops "range(1, 5)" \- 1 here is a position where range starts Simplest example of class macro usage: import pylib type HelloWorld = ref object name: string class HelloWorld(object): def init(self, name): self.name = name def greet(self): print("Hello,", self.name, sep = "\n") let c = newHelloWorld("Daniil") c.greet()
Re: Estimation of π using Leibniz series
It seems that Julia JIT is aware of SSE extensions and it uses them. GCC or MSVC should be emitting them too, but it depends on C code
Gource visualization of the Nim repo history
If you don't want the music, just turn it off History from 2008 to 2017 [https://www.youtube.com/watch?v=rUJ7Bzgv3n0](https://www.youtube.com/watch?v=rUJ7Bzgv3n0)
Re: thounghs about Nim language in godot
DTxplorer: don't worry, nothing bad
Re: thounghs about Nim language in godot
DTxplorer, you need to install godot-nim package itself first
Re: thounghs about Nim language in godot
You can now use Godot-Nim with master branch of Godot (so newest version)
Re: thounghs about Nim language in godot
Author said that only one PR to godot engine left
Re: thounghs about Nim language in godot
>From the example: # Copyright 2017 Xored Software, Inc. import strutils import godot import engine, label, resource_loader, packed_scene gdobj FPSCounter of Label: var lastFPS: float32 method ready*() = setProcess(true) method process*(delta: float64) = let fps = getFramesPerSecond() if int(fps * 10) != int(lastFPS * 10): lastFPS = fps self.text = "FPS: " & formatFloat(fps, ffDecimal, 1)
Re: thounghs about Nim language in godot
[https://github.com/pragmagic/godot-nim](https://github.com/pragmagic/godot-nim) Yay, proper GDNative bindings!!!
Re: echo proper string in different consoles?
There's a simpler way to change the codepage import os discard execShellCmd("chcp 936")
Nim support on repl.it
Hi everyone! repl.it started a vote poll called "Language requests". Since repl.it already supports C and C++, I think it would be not hard for them to add Nim support. Vote [here!](https://replit.canny.io/languages-requests/p/nim)