Re: Error converting json to types

2019-12-23 Thread sergio
Running into this, still seems to be a bug.

I have a simple type:


type
  Torrent* = object
  name*: string
  uploaded_at*: DateTime


Run

And when I try to render a seq[Torrent] in my jester API as json:


Users/sergiotapia/Work/torrentinim/src/torrentinim.nim(18, 14) 
template/generic instantiation of `%*` from here
/Users/sergiotapia/.choosenim/toolchains/nim-1.0.4/lib/pure/json.nim(331, 
36) template/generic instantiation of `%` from here
/Users/sergiotapia/.choosenim/toolchains/nim-1.0.4/lib/pure/json.nim(371, 
14) template/generic instantiation of `%` from here
/Users/sergiotapia/.choosenim/toolchains/nim-1.0.4/lib/pure/json.nim(364, 
41) Error: type mismatch: got 
but expected one of:
proc `%`(s: string): JsonNode
  first type mismatch at position: 1
  required type for s: string
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(n: uint): JsonNode
  first type mismatch at position: 1
  required type for n: uint
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(n: BiggestInt): JsonNode
  first type mismatch at position: 1
  required type for n: BiggestInt
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(n: BiggestUInt): JsonNode
  first type mismatch at position: 1
  required type for n: BiggestUInt
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(n: int): JsonNode
  first type mismatch at position: 1
  required type for n: int
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(n: float): JsonNode
  first type mismatch at position: 1
  required type for n: float
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(b: bool): JsonNode
  first type mismatch at position: 1
  required type for b: bool
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode
  first type mismatch at position: 1
  required type for keyVals: openArray[tuple[key: string, val: JsonNode]]
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
template `%`(j: JsonNode): JsonNode
  first type mismatch at position: 1
  required type for j: JsonNode
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`[T](elements: openArray[T]): JsonNode
  first type mismatch at position: 1
  required type for elements: openArray[T]
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`[T](table: Table[string, T] | OrderedTable[string, T]): JsonNode
  first type mismatch at position: 1
  required type for table: Table[system.string, %.T] or 
OrderedTable[system.string, %.T]
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`[T](opt: Option[T]): JsonNode
  first type mismatch at position: 1
  required type for opt: Option[%.T]
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`[T: object](o: T): JsonNode
  first type mismatch at position: 1
  required type for o: T: object
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(formatstr, a: string): string
  first type mismatch at position: 1
  required type for formatstr: string
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(formatstr: string; a: openArray[string]): string
  first type mismatch at position: 1
  required type for formatstr: string
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(f: string; t: StringTableRef; flags: set[FormatFlag] = {}): string
  first type mismatch at position: 1
  required type for f: string
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(o: enum): JsonNode
  first type mismatch at position: 1
  required type for o: enum
  but expression 'o.zonedTimeFromTimeImpl' is of type: proc (x: Time): 
ZonedTime{.closure, gcsafe, locks: 0.}
proc `%`(o: ref object): JsonNode
  first type mismatch 

Re: Error converting json to types

2019-12-23 Thread sergio
It seems like there needs to be a proc `%`(o: DateTime): JsonNode in the stdlib 
somewhere, but it was never implemented.


Error: 'solve' doesn't have a concrete type, due to unspecified generic parameters.

2019-12-23 Thread spip
In [my code](https://play.nim-lang.org/#ix=25jt), the compiler does not like 
the way I return the `solve` closure iterator with generic parameters and 
prints the error `'solve' doesn't have a concrete type, due to unspecified 
generic parameters.`

I don't understand the reason for such message, as both generic parameters are 
instantiated by the call of the enclosing proc.

Also, note that if you remove the second generic parameter of the `solve` 
iterator, using the following code, now the program compiles... 


...
  iterator solve[T]: Solution[T] {.closure.} =
yield solution
  
  try:
return solve[T]
...


Run

Of course, in the original code, I need both `T` and `V` generic parameters in 
real `solve`.


Re: Object Variants and some issues

2019-12-23 Thread FernandoTorres
Thanks @araq, how about allowing that pragma {.fieldChecks: on|off.} in the 
prog header, so that it would only affect its own scope?


Re: Problem with C interop/X11 bindings

2019-12-23 Thread SolitudeSF
nim implicitly dereferences pointers, there is no difference between these 
snippets.


Re: How to set a specific file as main file in VS Code ?

2019-12-23 Thread kcvinu
Thanks for the answer. Can you please explain the details of that process ? I 
am completely ignorant about that.


Re: Object Variants and redefinition of labels or reusbility of field names

2019-12-23 Thread FernandoTorres
Thanks @mashingan. My intention is to bring up a point (that popped up in an 
earlier post that I can't find) in which @araq and others that I can't 
remember, were talking about flexing out the restrictions, to allow duplication 
of either field labels or field names, when used in the case discrimination 
block for object variants. I could not find the post to append this case though.

In fact I made my example short enough for the forum, but I was trying to 
reduce a longer set of object types that looks like your first workaround, into 
a single object variant. Nesting objects is for sure what works since the 80's 
and 90's (i.e. I used it a lot in Turbo Pascal). The issue is that then you 
require double indexing like pointA.cyl.phi, instead of something like 
polarA.phi, but I can live with that.

Your second workaround is kind of cumbersome due to the need of exceptions, but 
i'll explore more about such approach. It gave me some enlightenment in an 
operator overload like x= (I didn't know you could use a proc name with it, and 
that's a nice feature.

What I'm trying to do, is to merge a large group of coordinate systems (not 
only 3D but from 1-D to N-D) into a single one, while exploring the 
possibilities in Nim, and that's how I got stuck. So it gets larger and larger 
as you start implementing (I have not yet, but a good initial type definition 
is the clue for a nice and intuitive library). Here the idea, still in its 
infancy:


type
  
#===
  #   COORDINATE SYSTEM NOTATION  RULE   DIMBUsPRINT 
($) (ARGUMENTS)NOTES
  
#===
  CoordinateSystem* = enum
csC1D,  # Cartesian 1-D or Line Number __ RHR __ 1-D __ L   __ (x) 
_ (x)  [  ]
csC2D,  # Cartesian 2-D or Polar  RHR __ 2-D __ LL  __ (x, y) 
__ (x, y) _ [  ]
csC3D,  # Cartesian 3-D _ RHR __ 3-D __ LLL __ (x, y, 
z) ___ (x, y, z) __ [  ]
csCar,  # Cartesian N-D _ RHR __ N-D __ L-L __ (x0, 
x1,... xN-1) ___ (x[0] .. x[n-1]) ___ [ x0=x, x1=y, x2=z ]
csPol,  # Polar _ RHR __ 2-D __ LA  __ (r, φ) 
__ (r, phi) ___ [ φ=azimuthal_a]
csCyl,  # Cylindrical ___ RHR __ 3-D __ LAL __ (r, φ, 
z) ___ (r, phi, z)  [ φ=azimuthal_a]
csSph,  # Spherical _ RHR __ 3-D __ LAA __ (ρ, θ, 
φ) ___ (rho, theta, phi) __ [ θ=polar_a, φ=azimuthal_a ]
  #[not defined (yet)
# Orthogonal 2-D candidates
csLin,  # Line Number ___ RHR __ 1-D __ L   __ (x) 
_ (x)  [ csLin=csC1D  ]
csPla,  # Planar  RHR __ 2-D __ LL  __ (x, y) 
__ (x, y) _ [ csPla=csC2D  ]
csEuc,  # Euclidean _ __ __ __  

csOrt,  # Orthogonal  __ __ __  

csCur,  # Curvilinear ___ __ __ __  

csHSp,  # Hyperspherical  __ __ __  

csPar,  # Parabolic 2-D _ __ __ __  

csEll,  # Elliptic 2-D __ __ __ __  

cs1CB,  # One Center Bipolar  __ 2-D __ LA  __ (σ, τ) 
__ (sigma, tau)
cs2CB,  # Two Center Bipolar  __ 2-D __ LL  __ (r1, r2) 
 (r1, r2)
csBAn,  # Biangular _ __ 2-D __ AA  __ (θ1, θ2) 
 (thheta1, thheta2)
csLoP,  # Logaritmic Polar __ __ 2-D __ LA  __ (ρ, θ) 
__ (rho, theta)
csHom,  # Homogeneous ___ __ 3-D __ LLL __ (x : y : 
z) _ (x, y, z)
csGeo,  # Geographic or Geodesic  __ 3-D __ AAL __ (φ, λ, 
h) ___ (phi, lambda, h) or (lat, lon, ele)
csHor,  # Celestial Horizontal __ __ 2-D __ AA  __ (a, A) 
__ (al, az)
cdEqu,  # Celestial Equatorial __ __ 2-D __ AA  __ (δ, α) 
__ (delta, alpha)
csEcl,  # Celestial Ecliptic  __ 2-D __ AA  __ (β, λ) 
__ (beta, lambda)
csGal,  # Celestial Galactic  __ 2-D __ AA  __ (b, l) 
__ (b, l)
csSup   

Issues with ICL compiling

2019-12-23 Thread DIzer
Hello, everybody

1\. I try to compile and bootstrap NIM with Intel C++ compiler (Intel parallell 
studio update 4, windows 10 64bit) strictly following to the recomendations.. 
The koch was compiled successfully, but bootstrapin' was not - the compilation 
was hung upon docgen.nim

  * is it possible to compile the whole thing with icl?



2.I try to compile the hello world program (echo "hello world!") with icl - it 
was successfull , but the size of the program - 290kb... that looks a bit 
oversized to me.. is it OK?


Re: How to set a specific file as main file in VS Code ?

2019-12-23 Thread treeform
Same. I have both run.sh and run.bat for windows or posix dev.


Re: How to parse RSS feeds with Nim

2019-12-23 Thread sergio
Thanks!

I don't know how to manually set the article excerpt in Hugo, but I'll try to 
look into it at some point.


Re: Walking trees without recursive iterators

2019-12-23 Thread cblake
> What do you think?

I agree that at least a few others would appreciate some fully worked out 
general tree iterator example and I encourage you to undertake the challenge.

A possibly helpful side-comment is that Python didn't really have recursive 
generators for the whole 1990s, but had a similarly nicely terse `for`-loop 
syntax for iteration. Then it grew `for x in sameItr: yield x` capability and 
finally `yield from`. So, there may be example code from back then to work off 
of.

Yet another possibility besides the several I mentioned and Araq's already 
mentioned visitor with a callback idea, would be to define an object holding 
all the state you need with a `next` method. Then the iterator could just 
instantiate one of those and call the next method until done. It would also be 
nice to be able to iterator forwards as well as backwards.

Another side-comment pertaining to @rayman22201's "parent node" idea is that 
while it can make tree navigation easier, for multi-way trees similar to your 
XML case, this can be undesirable. The reason is that when the parent changes 
many nodes need to have their parent field updated. While one frequently sees 
red-black or AVL trees with a parent node, one almost never sees B-trees with a 
parent node due to that update cost. Of course, there's also the storage cost 
(N-nodes with parents instead of a depth-proportional stack).


Re: Object Variants and some issues

2019-12-23 Thread Araq
> So my question is, can that pragma be documented?

Consider it done.


Re: Problem with C interop/X11 bindings

2019-12-23 Thread mashingan
> there is no nil, it reports wrong size.

I remember something when doing ffmpeg c binding. At first I wrote the binding 
without adding the header pragma, and just simply importc pragma only. In the c 
header file, that particular object actually internal struct that isn't 
included in dev headers, and it's there just as to declare the type/struct.

I debugged that and it was returning nil or something that not nil but would 
crash/segfault if I tried to deref it.

After adding the header pragma then it ran smoothly and when debugging, the gdb 
returning the correct struct name.

Idk if it's related or not, but you can try looking that part.


Re: Why is -1 mod 5 not equal to 4? (it is -1)

2019-12-23 Thread forcefaction
Thank you for the detailed explanation.


An overview of nimble.packages

2019-12-23 Thread marks
I wanted to get an overview of the nimble packages but there isn't a browse 
function, so I wrote the nim program below. To use it you need to download 
`packages.json` from [nimble.packages](https://github.com/nim-lang/packages/). 


{.experimental: "codeReordering".}

# TODO download the JSON file if mtime is older than today

import algorithm
import json
import os
import sequtils
import strformat
import strutils except escape
import sugar
import unicode
import xmltree

const Outfile = "nimble-packages.html"

type
  Entry = object
name: string
url: string
tags: seq[string]
description: string
license: string
web: string

main()

proc main() =
  let filename = getFilename() # May not return
  let entries = entryItems(filename)
  writeHtmlTable(entries)

proc getFilename(): string =
  let args = commandLineParams()
  if len(args) == 0 or args[0] == "-h" or args[0] == "--help":
echo("usage: ", lastPathPart(getAppFilename()),
 " /path/to/nimble.packages.json")
quit()
  args[0]

proc entryItems(filename: string): seq[Entry] =
  for item in parseJson(readFile(filename)):
var entry: Entry
try:
  entry = to(item, Entry)
except KeyError:
  for tag in ["name", "url", "description", "license", "web"]:
try:
  let node = item[tag]
  let text = node.getStr()
  case tag
  of "name": entry.name = text
  of "url": entry.url = text
  of "description": entry.description = text
  of "license": entry.license = text
  of "web": entry.web = text
except KeyError:
  discard # ignore missing for now
  try:
let node = item["tags"]
if node.kind == JArray:
  var tags = newSeq[string]()
  for element in node.getElems():
tags.add(element.getStr())
  entry.tags = tags
  except KeyError:
discard # no tags
if entry.name == "":
  continue
entry.tags = sorted(entry.tags.mapIt(it.toLower()))
result.add(entry)
if entry.url == "":
  entry.url = entry.web
  result.sort((a, b) => cmpIgnoreCase(a.name, b.name))

proc writeHtmlTable(entries: seq[Entry]) =
  var file = open(Outfile, fmWrite)
  defer: close(file)
  file.write("\n")
  file.write("Name/LinkDescriptionTags" &
 "License\n")
  for entry in entries:
let name = &"{escape(entry.name)}"
let description = escape(entry.description)
let tags = escape(join(entry.tags, ", "))
let license = escape(entry.license)
file.write(&"{name}{description}" &
   &"{tags}{license}\n")
  file.write("\n")
  file.write(&"{len(entries)} packages.\n")


Run


Re: Problem with C interop/X11 bindings

2019-12-23 Thread dariolah
As far as I can tell, **group_info** values differ


desc.map.key_sym_map[keycode].group_info


Run


desc->map->key_sym_map[keycode].group_info


Run


Re: How to parse RSS feeds with Nim

2019-12-23 Thread ducdetronquito
Simple and straight to the point article, good job :)

Ps: On your home page, the excerpt does not display nicely as it shows a 
portion of the RSS feed in the article.


Re: Object Variants and some issues

2019-12-23 Thread solo989

proc `$`(mo: MyObj): string =


Run

doesn't need to be surrounded by field checks off. You have a bug in your code. 


of mk2: result &= "u:" & $mo.x & ", v:" & $mo.y & ", w:" & $mo.z


Run

should be 


of mk2: result &= "u:" & $mo.u & ", v:" & $mo.v & ", w:" & $mo.w


Run


Re: Why is -1 mod 5 not equal to 4? (it is -1)

2019-12-23 Thread Stefan_Salewski
> I know that there is mod and %%.

There is also floorDiv() and floorMod(), maybe you like that better:

[https://nim-lang.org/docs/math.html#floorDiv%2CT%2CT](https://nim-lang.org/docs/math.html#floorDiv%2CT%2CT)