Re: Deprecation warnings

2019-12-04 Thread Araq
You can simply use `uint` (or `int`) instead which works with every Nim version.


Re: Sublime Text 3 support

2019-12-04 Thread e
Yes, @Hendrik, Nimlime uses the nim that is installed and on your PATH.


Re: Sublime Text 3 support

2019-12-04 Thread Hendrik
Hi e,

Well what I wanted was the latest version of NIM in ST. But I believe it might 
be my lack of understanding on how Nimline works. For instance, I thought that 
Nimline includes a version of NIM that it runs but it seems from what you are 
saying is that it alows you to use NIM in any version in ST is that correct?


Re: generic typevariable binding

2019-12-04 Thread e
Thanks @mratsim \-- I'll use your `weight = V(1)` suggestion.

I'm sorry you had to fill in missing bits... the failing code is 
[here](https://github.com/dcurrie/AdventOfCode/tree/master/Utils)

It fails if you uncomment the test case in `dijktra.nim` and try `nim c -r 
dijktra.nim`


Re: {.gcsafe.}, What does {.threadvar.} do to make a function gcsafe?

2019-12-04 Thread mratsim
Iirc the gcsafe requirement was made so that it would not be breaking in the 
future to make async threadsafe.


Re: Mix of types in division operator '/'

2019-12-04 Thread mratsim
> as it both leaves the div keyword to be used for a different purpose that is 
> more appropriate and necessary

Maybe for you, but I think division is a necessary operation.

Besides div is not a keyword, you can overload it:


proc `div`(body: string): string =
  "\n" &
"  " & body & "\n" &
  ""


Run


Re: Set object field from strings at run time?

2019-12-04 Thread mratsim
Approximate fields setter and getter with JsonNode and some experimental 
dooperator magic (from 
[https://forum.nim-lang.org/t/5000#31345](https://forum.nim-lang.org/t/5000#31345)
 and 
[https://github.com/status-im/nim-cookbook/blob/master/dynamic_approximating_dynamic_types.nim](https://github.com/status-im/nim-cookbook/blob/master/dynamic_approximating_dynamic_types.nim))


import json

{.experimental: "dotOperators".}

type
  Action = ref object
properties: JsonNode

template `.`(action: Action, field: untyped): untyped =
  action.properties[astToStr(field)]

template `.=`(action: Action, field, value: untyped): untyped =
  action.properties[astToStr(field)] = %value


# Our main object, the fields are dynamic

var a = Action(
  properties: %*{
"layer": 0,
"add": true,
"vis": false,
"new_name": "fancy_name"
  }
)

# And usage, those are not real fields but there is no difference in syntax

echo a.new_name # "fancy_name"

a.algo = 10
echo a.algo # 10


Run


Re: generic typevariable binding

2019-12-04 Thread mratsim
The example is missing a bit of proc for reproduction but this compiles for me 
(though it segfaults at runtime after).


import sets, tables, hashes

type
  DEdge*[K, V] = ref object
fm*: DNode[K]
to*: DNode[K]
weight*: V
  
  DNode*[K, V] = ref object
key*: K
outedges: HashSet[DEdge[K, V]]
inedges:  HashSet[DEdge[K, V]]
priority*: V # for algorithms to sort upon
index*: int  # for algorithms to store sort position
  
  DGraph*[K, V] = object
# A directed graph of nodes and directed edges
nodes: Table[K, DNode[K, V]]

proc hash(x: DEdge): Hash =
  discard

proc add_edge*[K, V](graph: var DGraph[K, V], fm: K, to: K, weight: V = 1): 
DEdge[K, V] =
  # var n1 = graph.add_node(fm)
  # var n2 = graph.add_node(to)
  # result = DEdge[K, V](fm: n1, to: n2, weight: weight)
  result.fm.outedges.incl(result)
  result.to.inedges.incl(result)

proc add_edges*[K, V](graph: var DGraph[K, V], edges: seq[(K,K,V,)]): 
seq[DEdge[K, V]] =
  for (n1, n2, w) in edges:
result.add(add_edge(graph, n1, n2, w))

var g = DGraph[int,float]()
let _ = g.add_edges(@[(3,4,3.5),(4,5,4.5)])


Run

Note that to avoid potential false inference I would change the `add_edge` 
signature from `weight: V = 1` to `weight = V(1)`


Re: Deprecation warnings

2019-12-04 Thread mratsim
The proc that now accepts csize_t instead of csize should have a deprecated 
overload to ease the transition (or a converter but converters tend to create 
hidden bugs in my opinion)


Re: CountTable inconsistency?

2019-12-04 Thread onde
Created an issue [on Github](https://github.com/nim-lang/Nim/issues/12813)


Re: CountTable inconsistency?

2019-12-04 Thread onde
Thanks Araq. Just to explain: I'm reading a user provided histogram, which is 
by right a CountTable, but I need to delete certain keys post ingestion. Since 
CountTable doesn't provide a del() I had to resort to setting the key to 0, 
which surfaced the bug above.


Re: CountTable inconsistency?

2019-12-04 Thread Araq
Sounds like a bug to me but a count table is for counting. Setting a value to 0 
is not counting, you're doing something else, use an ordinary table.


generic typevariable binding

2019-12-04 Thread e
With a `type` that depends on two generic typevars, the type inference 
implementation seems to get stuck on a particular binding of the second typevar 
based on the first... is that expected?

Specifically, for these two sections:


var w = DGraph[int,int]()
let _ = w.add_edges(@[(0, 1, 10), (0, 4, 3), (1, 2, 2), (1, 4, 4)])

Run


var g = DGraph[int,float]()
let _ = g.add_edges(@[(3,4,3.5),(4,5,4.5)])

Run

the second one fails with `Error: type mismatch: got  but expected 'DNode[system.int, system.int]'`

If I reverse the sections, I get a similar failure with the `got` and 
`expected` types switched.

However, these work fine following either of the above:


var h = DGraph[uint,float]()
let _ = h.add_edges(@[(3u,4u,3.5),(4u,5u,4.5)])

Run


var k = DGraph[char,int]()
let _ = k.add_edges(@[('s','a',3),('s','c',2),('s','f',6)])

Run

The only thing that distinguishes the failure seems to the type taken on by the 
first typevar.

Help!?

Thanks.

\-- e

Below are more details:


type
  DEdge*[K, V] = ref object
fm*: DNode[K]
to*: DNode[K]
weight*: V
  
  DNode*[K, V] = ref object
key*: K
outedges: HashSet[DEdge[K, V]]
inedges:  HashSet[DEdge[K, V]]
priority*: V # for algorithms to sort upon
index*: int  # for algorithms to store sort position
  
  DGraph*[K, V] = object
# A directed graph of nodes and directed edges
nodes: Table[K, DNode[K, V]]

Run

and


proc add_edges*[K, V](graph: var DGraph[K, V], edges: seq[(K,K,V,)]): 
seq[DEdge[K, V]] =
  for (n1, n2, w) in edges:
result.add(add_edge(graph, n1, n2, w))

proc add_edge*[K, V](graph: var DGraph[K, V], fm: K, to: K, weight: V = 1): 
DEdge[K, V] =
  var n1 = graph.add_node(fm)
  var n2 = graph.add_node(to)
  result = DEdge[K, V](fm: n1, to: n2, weight: weight)
  result.fm.outedges.incl(result)
  result.to.inedges.incl(result)

proc add_node*[K, V](graph: var DGraph[K, V], key: K): DNode[K, V] =

Run


UI showcase ideas

2019-12-04 Thread filcuc
I would like to showcase the potential of my nimqml bindings. Abu idea for a 
possibile usefull app? Yet another to do list?


Re: Sublime Text 3 support

2019-12-04 Thread e
[Nimlime](https://github.com/Varriount/NimLime) works for me, at least for 
syntax(-error) highlighting and jump to definition. It has not been updated for 
about a year, but I've had no issues, though there are some listed on github. 
What trouble are you having?


Re: Any possibility of a near term Lazarus/C#-like GUI-designer IDE?

2019-12-04 Thread juancarlospaco
The problem is actually choose which one, they all look cool in its own way. 浪 


Re: Any possibility of a near term Lazarus/C#-like GUI-designer IDE?

2019-12-04 Thread treeform
I am trying to make a UI based tool that use Figma to draw the UIs. Idea is 
that you draw your UI in Figma and export it to run with Fidget: 
[https://github.com/treeform/fidget](https://github.com/treeform/fidget) . I 
hope to target HTML, Win, Mac, Linux, Android and iOS.


Re: Any possibility of a near term Lazarus/C#-like GUI-designer IDE?

2019-12-04 Thread stph
Thank you all for the friendly and very encouraging responses. 
Congratulations,also, to the developers for your recent 1.0 achievement! I 
expect I would have looked at Nim before now (as all I've ever heard about the 
effort has been good) but there's typically a huge gap between alpha and 1.0, 
which most languages never bridge successfully.

I should have a few weeks of free time after the 20th and I will attempt to, at 
least, get some basic orientation.

So are NimQML and NimX the two safest bets for a noob right now? Native look 
and feel doesn't matter to me, but an easy process of install and getting a 
first widget working with Nim are very important. I'll be working on a Mac and 
on Mint Linux; it looks like those are both covered by package managers -- so 
that is very encouraging. If I can get to the point where the UI is basically 
working I'll be very encouraged to redouble my time; for some reason I have a 
tough time motivating myself through the learning of a new language if I can't 
get something "real", even if quite humble, working pretty quickly. I tried to 
learn Haskell once, but 2 weeks in I was still agonizing over the pretty much 
mandatory libraries...and then I hit the Monad! :-/

Thanks again for the advice!


CountTable inconsistency?

2019-12-04 Thread onde
Hi all,

new forum member here. Please be forgiving.

I think I discovered an inconsistency in CountTable. I couldn't find a way to 
delete a key (no del() implementation?!), so instead I set its value to 0 as 
recommended in [this Stackoverflow 
Post](https://stackoverflow.com/questions/59160984/remove-key-from-counttable-in-nim/59172084?noredirect=1#comment104573660_59172084).
 While the key now disappears from keys() it's still accounted for in len().

Crude example:


import tables

let myString = "abracadabra"
var letterFrequencies = toCountTable(myString)

echo "BEFORE set 0"
echo letterFrequencies
echo "len ", len(letterFrequencies)
for k, v in letterFrequencies.pairs():
  echo k, ":", v

echo "AFTER set 0"
letterFrequencies['a'] = 0
echo letterFrequencies
echo "len ", len(letterFrequencies)
for k, v in letterFrequencies.pairs():
  echo k, ":", v

Output:


BEFORE set 0
{'a': 5, 'b': 2, 'c': 1, 'd': 1, 'r': 2}
len 5
a:5
b:2
c:1
d:1
r:2
AFTER set 0
{'b': 2, 'c': 1, 'd': 1, 'r': 2}
len 5
b:2
c:1
d:1
r:2

Is this the expected behaviour?

Many thanks,

Andreas

PS: Nim version 1.0.4


Re: Any possibility of a near term Lazarus/C#-like GUI-designer IDE?

2019-12-04 Thread sky_khan
> Has there been any thought of adapting it to self-reflectively work with Nim?

Well, I had played with this idea a bit. Then I've realized I cant reconcile 
their exceptions. I had to wrap all inter-calls with expensive try-except 
blocks or your program will crash at first run-away exception.


Re: Getting fields of an object type at compile time?

2019-12-04 Thread Vindaar
You might want something like this:


import macros

type Foo = object
  a: int
  b: string

macro getFields(x: typed): untyped =
  let impl = getImpl(x)
  let objFields = impl[2][2] # 2 is nnkObjectTy, 2 2 is nnkRecList
  expectKind objFields, nnkRecList
  result = nnkBracket.newTree()
  for f in objFields:
expectKind f, nnkIdentDefs
result.add nnkPar.newTree(newLit f[0].strVal, newLit f[1].strVal)

for (fieldName, typeName) in getFields(Foo):
  echo "Field: ", fieldName, " with type name ", typeName


Run

It returns a bracket of (field name, type name) tuples. Both as strings, since 
you can't mix strings with types in a tuple. For more complicated objects you'd 
have to recurse on the fields with complex types of course.


Re: Getting fields of an object type at compile time?

2019-12-04 Thread SolitudeSF
https://nim-lang.github.io/Nim/iterators.html#fieldPairs.i%2CT


Re: Getting fields of an object type at compile time?

2019-12-04 Thread pmags
@SolitudeSF \-- My understanding is `fieldPairs` only works on instances of a 
type, not on the`` typedesc`` itself.

`fieldPairs(Foo)` yields a type mismatch.


Getting fields of an object type at compile time?

2019-12-04 Thread pmags
Still on my journey to understand macros and the Nim AST...

Is there an easy way to get the fields of an object type at **compile time** in 
a macro? Or do I need to write another macro to walk the AST myself?

For example, given:


type Foo = object
  a: int
  b: string


Run

Can I get something like:


getFields(Foo)
("a", int), ("b", string)


Run


Deprecation warnings

2019-12-04 Thread Stefan_Salewski
With a fresh Nim devel I am getting


Hint: glib [Processing]
/home/stefan/gintrotest/tests/glib.nim(68, 13) Warning: use `csize_t` 
instead; csize is deprecated [Deprecated]
/home/stefan/gintrotest/tests/glib.nim(69, 12) Warning: use `csize_t` 
instead; csize is deprecated [Deprecated]
/home/stefan/gintrotest/tests/glib.nim(68, 13) Warning: use `csize_t` 
instead; csize is deprecated [Deprecated]
/home/stefan/gintrotest/tests/glib.nim(69, 12) Warning: use `csize_t` 
instead; csize is deprecated [Deprecated]
/home/stefan/gintrotest/tests/glib.nim(401, 42) Warning: Gsize is 
deprecated [Deprecated]
/home/stefan/gintrotest/tests/glib.nim(704, 48) Warning: Gsize is 
deprecated [Deprecated]
/home/stefan/gintrotest/tests/glib.nim(788, 47) Warning: Gsize is 
deprecated [Deprecated]
...


Run

and much more. I assume csize is deprecated, and while GSize is based on it, I 
get it for GSize too.

Well that are only warnings, but a few remarks:

For me the warning text is inverted in logic, why not "csize is deprecated, use 
csize_t instead". I am wondering about this since many years.

Then, I was just looking for when csize_t is available. Because when I replace 
csize with csize_t I want to ensure that it compiles for 1.0 users still. Have 
not found a hint in release logs.

And last note: May generally the deprecation warnings come too early? That is, 
why not introduce csize_t first, but do not deprecate csize at the same time? 
Maybe deprecate csize some months later, when really all users have a compiler 
with csize_t available. Because: Most people use devel compiler, and so get all 
the deprecation warnings be default, but maybe can not fix the code, because a 
few users still have old compiler.


Re: An error has occurred when I ran `nimble publish`

2019-12-04 Thread chocobo333
Thanks for your advice.

The command succeeded after updating nim.


Re: An error has occurred when I ran `nimble publish`

2019-12-04 Thread juancarlospaco
`nimble check` checks your package to see if its Ok. Run the command again. 
Check that you are using Nim `1.0.4`. 


Re: Mix of types in division operator '/'

2019-12-04 Thread juancarlospaco
[https://github.com/Yardanico/nimpylib#nimpylib](https://github.com/Yardanico/nimpylib#nimpylib)
 already has that. 


An error has occurred when I ran `nimble publish`

2019-12-04 Thread chocobo333
Hi. I am beginner of nim and english.

I ran 


nimble publish


Run

and then an error has occurred. 


Pushing to remote of fork.
   Info Creating PR
httpclient.nim(271)  httpError
Error: unhandled exception: Connection was closed before full request has 
been made [ProtocolError]


Run

Please tell me why the error occurred, and how should I do.

Should I run `nimble publish` again?


Re: Mix of types in division operator '/'

2019-12-04 Thread Araq
So learn some Nim before complaining:


template `//`(a, b: untyped) : untyped = a div b


Run

I couldn't copy Python's design because back then Python only had `/` and it 
sucked so I had to copy a different design.


Re: Any possibility of a near term Lazarus/C#-like GUI-designer IDE?

2019-12-04 Thread Araq
Ok, please ignore what I said. :D


Re: Any possibility of a near term Lazarus/C#-like GUI-designer IDE?

2019-12-04 Thread juancarlospaco
Theres UI Builder. NimQML can use the tools for Qt5 too.

I recently try all the most popular GUI libs on Nimble, they all had updates <1 
year, they all work and compile a Hello World or Example GUI provided.

NimX is great is fast and compiles to lots of platforms, has a YAML like DSL, 
GPU render.

Webview is simple and tiny, just 1 .nim and 1 .h file, GUI with JavaScript ala 
Electron, you can use Nim target JavaScript for it too, GPU render?, few Kb 
hello world, no dependencies, no DSL but Nim Frontend has DSL so you can just 
use that DSL anyways.

NimQML are Qt5 QML native widgets for Nim, uses a C lib, is fast and even takes 
the desktop themes, GPU render?, 42kb hello world, no DSL but QML is like 
CSS-ish.

UI Builder already described above.

And theres more on Nimble too. So it seems theres good GUI support. 路‍♀️


Re: Godot and Blender

2019-12-04 Thread juancarlospaco
I tried the Nim Godot bindings and they compiled like >=2 month ago.

Blender uses Python so using it from Nim should be doable with Nimpy. 


Re: {.gcsafe.}, What does {.threadvar.} do to make a function gcsafe?

2019-12-04 Thread mp035
Thank you, that explanation makes sense regarding {.threadvar.} however I am 
now confused as to why I need to worry about it at all. I am using **async** 
http server which I am assuming is concurrent (single threaded), so everything 
should be running in the same thread. This would explain why the threadvar 
state is consistent across my application, but does not explain why I need to 
worry about threadvars. Do you know why threadvar is even required?


Re: Advent of Nim 2019 megathread

2019-12-04 Thread me7
First year of AoC and first year of Nim too. Each part I usually take 2 hours 
so I need dedicate 4 hours each day to finish it. 
[https://github.com/me7/AdventOfCode](https://github.com/me7/AdventOfCode)

Looking at other people code once I've done it is my favorite part. I've learn 
what's my mistake everyday


Re: {.gcsafe.}, What does {.threadvar.} do to make a function gcsafe?

2019-12-04 Thread kidandcat
The Threadvar pragma, as its name explains, marks the variable as a thread 
variable, then each thread you create will have its own copy of that variable, 
thus, even if you error has dissapeared, you will find that if you modify that 
variable in your thread A, and you then read it in your thread B, you will find 
that it will be empty in B because A has written in its own copy, not in B's 
copy of the variable.


Re: Godot and Blender

2019-12-04 Thread kidandcat
There are existing bindings (maybe they are outdated)

[https://github.com/pragmagic/godot-nim](https://github.com/pragmagic/godot-nim)


Re: Any possibility of a near term Lazarus/C#-like GUI-designer IDE?

2019-12-04 Thread Araq
We had example code showing how to import the produced object files into a 
Lazarus project, this code later got removed as we have to ensure our examples 
continue to work. ;-) But it's definitely possible to do that but then you have 
the usual problems of mixing languages: FPC's strings are not Nim's, 
TStringList is not `seq[string]` etc.

So a Nim specific solution would be nice and while there are a couple of Nimble 
UI packages they all seem to lack a UI builder. That is not as bad as it sounds 
though as Nim focuses on DSLs so usually you get some DSL for building UIs 
quickly. For this to become more viable incremental compilation and "hot code 
reloading" are important and these **are** on our agenda, see 
[https://github.com/nim-lang/RFCs/milestone/1](https://github.com/nim-lang/RFCs/milestone/1)

But the real problem IMO is that desktop UI development has become a niche to 
the point where it's often cheaper (= more developers know the technology) to 
develop web applications even if it's just for internal business needs...


Re: Godot and Blender

2019-12-04 Thread Hendrik
Hey awesome didn't know that about Godot!

I will ask them maybe someone would be interested thanks for the reply.


{.gcsafe.}, What does {.threadvar.} do to make a function gcsafe?

2019-12-04 Thread mp035
Hi, I'm working with asynchttpserver and I found that I can not access global 
variables from within the server callback.

This seems to be because the server requires a {.gcsafe.} callback, and if a 
global variable (in this case a table) is accessed from within the callback it 
is no longer gc safe.

Adding the {.threadvar.} pragma to the global variable fixed the error, but I 
don't know why. So:

  1. How does accessing global variables make a function not gcsafe?
  2. What does the threadvar pragma do to fix this?



Thanks.


Mix of types in division operator '/'

2019-12-04 Thread jordyscript
What is referred to here as integer division is also known as floor division.

The Python language offers normal division that returns a floating point number 
through the / operator and integer/floor division through the // operator.

I find this much more elegant, as it both leaves the div keyword to be used for 
a different purpose that is more appropriate and necessary (think about how 
Karax goes about its business of defining divs), and it looks more mathematical 
by using a symbol instead of a keyword. It is also more concise being just two 
characters.