Re: Use getenv with js

2020-06-22 Thread Araq
The error message could be better, of course, but JS has no `getEnv` and Nim's 
JS target is **not** NodeJS.


Re: New Fixed-Point Number Library

2020-06-22 Thread lbart
Thank you for your comments!

Nice idea, I'll take a look at in.


Re: Can't install the bin tarball of nim 1.2.2.

2020-06-22 Thread halloleo
Ups, sorry @araq! Totally forgot! You did mentioned this in your first reply. 
I'll try it.


Newbie question: why do I get a "No handles or timers registered in dispatcher" error?

2020-06-22 Thread alastair
I've simplified my issues down to a simple test case where I use spawn() to run 
a proc that completes a future. It feels like it ought to be straightforward:


import asyncdispatch
import threadpool

let future = newFuture[string]()

let complete_future = proc (future_to_complete:Future[string]) =
future_to_complete.complete("woah")

spawn complete_future(future)
let str = waitFor future
echo str


Run

But on the waitFor line I get the following exception:

> Error: unhandled exception: No handles or timers registered in dispatcher. 
> [ValueError]

I'm a total newbie so I'm sure I'm missing something key here. But what is it?


Use getenv with js

2020-06-22 Thread vsantana
Hi everyone! when I try to compile this code into js (e.g. nim js main.nim)


import os
echo getEnv("USER")

Run

I'm getting the follow error: 


.choosenim/toolchains/nim-1.2.2/lib/pure/includes/osenv.nim(97, 16) Error: 
internal error: expr(nkBracketExpr, tyUncheckedArray)

Run

Was this supposed to work?


Re: More fuzz testing

2020-06-22 Thread quetzalb
printSeq is writing to stdout itself before returning a default value empty 
string.


Re: More fuzz testing

2020-06-22 Thread neo
I would expect


print first 1 3 5 7
printSeq(seq1 ,2) 1 3


Run


Re: "subsequence" type (like Go's "slice")

2020-06-22 Thread bpr
> There is also openArray. It's currently restricted to parameters due to 
> memory safety reasons, there is an RFC to extend it.

I assume you mean [this](https://github.com/nim-lang/RFCs/issues/178)? What's 
the status of that and the other 
[Nim2020](https://github.com/nim-lang/RFCs/milestone/1) RFCs? It has always 
said 0% complete on that page, and 2020 is about half over. I don't believe any 
of it is 0% complete ;-)


Re: More fuzz testing

2020-06-22 Thread SolitudeSF
what's the bug?


Re: Norm 2.0.0

2020-06-22 Thread Araq
Interesting. Can you outline the learned lessons?


Re: "subsequence" type (like Go's "slice")

2020-06-22 Thread snej
I'm writing code that manipulates portions of `seq``s, and I keep creating proc 
parameter lists that include ``input: openarray[byte]; inRange: var 
Slice[int]`, and then inside the proc I keep writing stuff like 
`input[inRange.a + i]` 

I have a large C++ codebase that uses a custom `slice` type that simply 
contains a pointer and a length, and I've found it extremely useful over the 
years; it really cleans up code that deals with byte ranges. (C++17's 
`string_view` is pretty much the same thing.) Of course it's very unsafe, but 
I'm used to that in C++. I'd like a safer version of it (more like Go's) in 
Nim. So I'm implementing one now; your toy sequence class from your ARC/ORC 
talk is proving very useful!


Re: Dual number type

2020-06-22 Thread whiterock
Thank you, this solves the first problem I had. I don't understand though, why 
it can infer N from Dual[N, T] but not T. Anyways, the problem remains that 
scalar multiplication should work for arbitrarily nested duals, i.e. 


let a = dual[2, Dual[2, float]](
  dual[2, float](1.0, [0.0, 0.0]),
  [dual[2, float](0.0, [0.0, 0.0]), dual[2, float](1.0, [0.0, 0.0])]
)


Run

but assuming the type T in Dual[N, T] can only be inferred if provided as 
another argument, I don't know what the function signature needs to be, so this 
fails: 


# Error: type mismatch: got 
echo 183.0*a


Run

The wanted output is the following just propagating the scalar multiplication 
through. An analogy I could think of would be viewing matrices as a vector of 
vectors where one can also propagate the scalar multiplication to the vectors. 
To be more clear: 


let want_expect = dual[2, Dual[2, float]](
  dual[2, float](183.0, [0.0, 0.0]),
  [dual[2, float](0.0, [0.0, 0.0]), dual[2, float](183.0, [0.0, 0.0])]
)


Run

Thanks in advance, ~whiterock


Nim Days Chapter 21: Parser combinators and Nim

2020-06-22 Thread aredirect
Here's a new chapter for one of my earliest code in nim nim-parsec

[https://xmonader.github.io/nimdays/day21_parsec.html](https://xmonader.github.io/nimdays/day21_parsec.html)


More fuzz testing

2020-06-22 Thread neo
Follows an edge case, an unexpected behaviour or a programmer's bug?


let sp = ' '
let seq1 = @[1, 3, 5, 7]

proc printSeq(s: seq, nprinted: int = -1) :string =
  var nprinted = if nprinted == -1: s.len else: min(nprinted, s.len)
  for i in 0 ..< nprinted:
write(stdout, s[i], sp)

write(stdout, "print first", printSeq(seq1), "\L")

echo "printSeq(seq1, 2)", printSeq(seq1, 2)

Run

ouputs


1 3 5 7 print first
1 3 printSeq(seq1 ,2)


Run


Norm 2.0.0

2020-06-22 Thread moigagoo
Hi all!

It is my great pleasure to announce Norm version 2.0.0 

I've rewritten in from scratch, because the previous architecture turned out to 
have unsolvable issues. There's zero backward compatibility, so if you've never 
used Norm before, just start using the new version and don't bother looking and 
previous versions, and if you do use Norm already, I'm afraid you'll have to 
rewrite your code.

But this sacrifice is not in vain! Norm 2.0.0 is much more production ready. It 
finally solves the n+1 problem, it lets you easily connect to as many DB 
connections as you want, and you finally can have your models anywhere you 
like. The behavior of DB procs is now more predictable, there are no more magic 
object construction on the fly. The syntax is now more explicit.

To learn the new Norm, [visit the 
docs](https://moigagoo.github.io/norm/norm.html).

And just to give you the taste of the new, Nim 1.2.0-driven API, here's how you 
filter and update rows (taken from 
[tsqlitesugar.nim](https://github.com/moigagoo/norm/blob/develop/tests/tsqlitesugar.nim#L40)):


discard @[newToy()].dup:
  dbConn.select("price > ?", 50)
  apply(doublePrice)
  dbConn.update


Run


Re: New Fixed-Point Number Library

2020-06-22 Thread b3liever
I think you could possibly make `fracbits` part of the type (that would mean 
you will have to use generics, everywhere). Like so: `type FixedPoint[fracBits: 
static[int]] = objectBut`. But `static` has many bugs yet so maybe it wouldn't 
work, just yet.


Re: New Fixed-Point Number Library

2020-06-22 Thread b3liever
Really nice fixed point lib, with docs, tests and a way to generate different 
representation, operators... It is very complete, well done!


"subsequence" type (like Go's "slice")

2020-06-22 Thread snej
As I get more fluent in Nim, the main tool I find myself missing is a 
lightweight reference to a range of bytes in memory — something that points to 
a portion of a `seq` without copying it. The closest equivalent I know of is 
Go's `slice[T]`, which is pretty ubiquitous — it's actually rare in Go to pass 
or return an array itself.

I'm assuming this doesn't exist in Nim itself; I've spent some time scanning 
through the standard library docs, esp. the `system` and `sequtils` modules, 
and I think I'd have found it if it were there. But there might be a Nimble 
package.

This could be implemented pretty simply, if non-optimally; something like


type subseq*[T] = object
parent: ref seq[T]
range: Slice[Natural]


Run

The "non-optimal" part is the double-indirection getting to the bytes; it needs 
a `ref seq` to establish ownership so the GC will keep the heap block alive. 
One could add a direct pointer to the start of the range, in which case 
`range.a` isn't needed, so we get


type subseq*[T] = object
parent: ref seq[T]
base: UncheckedArray[T]
len: uint


Run

Both of these become unsafe if the seq is mutated; obviously if it's shortened 
enough to invalidate the end of the range, and more subtly the second design's 
`base` is invalidated if the seq has to grow its buffer. Not sure how to deal 
with that — it seems to require a Rust-like restriction that a sequence can't 
be mutated while any slices on it are extant, but I don't think 
Nim-as-we-know-it has ways to enforce that.


Re: Fuzz testing nim

2020-06-22 Thread neo
Thanks saves me the report!


Re: Fuzz testing nim

2020-06-22 Thread Yardanico
Duplicate of 
[https://github.com/nim-lang/Nim/issues/13992](https://github.com/nim-lang/Nim/issues/13992)
 :)


Re: Fuzz testing nim

2020-06-22 Thread SolitudeSF
compiler crashes are not expected behavior. you should report it on github.


New Fixed-Point Number Library

2020-06-22 Thread lbart
Hi,

After having had a lot of fun learning and testing Nim, I have started working 
on several libraries, including one on FPN (and another on CAD/GIS geometry). 
[https://gitlab.com/lbartoletti/fpn](https://gitlab.com/lbartoletti/fpn)

A lot of the code comes from old notes that I had accumulated and coded in C, 
so the style may not be "NIMic" yet :)


Fuzz testing nim

2020-06-22 Thread neo

var seq1: seq = @[1, 3, 5, 7]

Run


Error: internal error: genTypeInfo(tyNone)
No stack traceback available
To create a stacktrace, rerun compilation with ./koch temp c 


Run

Is this an expected compiler behaviour?


Re: Custom HTML elements

2020-06-22 Thread geotre
Do you mean like web components? For SPAs have a look at Karax


Re: detectOs template in for loop

2020-06-22 Thread neo
Second macro version outputs


Ubuntu


Run

which is the correct answer!


Re: detectOs template in for loop

2020-06-22 Thread Araq
> But it's actually a tricky problem that might not be able to be solved with 
> an iteration of Distribution, which is probably why it's not in the stdlib.

distros is for Nimble support, for nothing else. Detecting the distro is 
fundamentally flawed anyway, test for features, not for distros or versions.


Re: detectOs template in for loop

2020-06-22 Thread neo
Thanks for the workaround.

As an aside, it turns out


for distro in Distribution:
  if detectOsImpl(distro): echo distro

Run

outputs two strings:


Posix
Linux


Run

The macro ouputs the first.

What is the benefit of only accessing detectOsImpl through a template?


Re: Sending emails from Nim using SMTP

2020-06-22 Thread enthus1ast
You can try to use my library:

[https://github.com/enthus1ast/nimMime](https://github.com/enthus1ast/nimMime)

it could be that you need to patch one or two things (pr welcome ;))


Re: detectOs template in for loop

2020-06-22 Thread jyapayne
Actually, the above won't work due to the general nature of Posix and Linux. 
This may be better:


import macros
import distros

macro findOS*(): Distribution =
  var body = newNimNode(nnkStmtList)
  
  
  for distro in Distribution:
if distro in {Linux, Posix}:
  continue
let distLit = ident($distro)
body.add(
  quote do:
if detectOS(`distLit`):
  return Distribution.`distLit`
)
  
  # We want to check these last because they are more general
  body.add(
quote do:
  if detectOS(Linux):
return Distribution.Linux
  if detectOS(Posix):
return Distribution.Posix
  )
  
  result = quote do:
(proc (): Distribution = `body`)()

echo findOS()


Run

But it's actually a tricky problem that might not be able to be solved with an 
iteration of Distribution, which is probably why it's not in the stdlib.


Re: Termcurs

2020-06-22 Thread JPLRouge
Update and harmonization for termkey.nim, for standard use of the source 
generator.

designer progression big update for termcurs.nim


Re: detectOs template in for loop

2020-06-22 Thread jyapayne
This is, unfortunately, not possible at the moment with the way the template is 
written. You can work around this, however, with the following macro:


import macros
import distros

macro findOS*(): Distribution =
  var body = newNimNode(nnkStmtList)
  
  # Mac is later in the enum than Posix, and
  # Mac is technically posix, so this is needed
  # to properly detect a Mac
  body.add(
quote do:
  if detectOS(MacOSX):
return Distribution.MacOSX
  )
  
  for distro in Distribution:
let distLit = ident($distro)
body.add(
  quote do:
if detectOS(`distLit`):
  return Distribution.`distLit`
)
  result = quote do:
(proc (): Distribution = `body`)()

echo findOS()


Run


How to debug a library with gdb on windows?

2020-06-22 Thread Shucks
So nim-gdb is working fine for me now with binaries. I can use it on VSCode 
aswell and it works great. finish.exe should set the NIM_GDB environment in the 
nim-gdb.bat if you let it setup mingw btw. I had to fix that manually.

However, I've coded a simple library with nim which gets load some time after 
the main program is launched. The library itself is compiled with 
\--debugger:native --passL:-s --passL:-static-libgcc. I've expected that 
attaching gdb to the process should be enough to set breakpoints but thats not 
the case.

So how to debug a library coded in nim?

My try was: 


nim-gdb -p pid
b main.nim:90
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (main.nim:83) pending.

Library gets load now and breakpoint never gets hit and stay on pending all 
the time.


Run


Re: Dual number type

2020-06-22 Thread jyapayne
Replace `x: SomeFloat;` with `x: T;` in your multiplication proc definition. 
The nim compiler can't determine which `T` you are wanting when there are no 
parameters with `T` as the type, so it's telling you that with `Maybe generic 
arguments are missing?`. 


Re: Dual number type

2020-06-22 Thread jyapayne
If you want just scalar, you can change the definition of `T` in this case to:


# scalar multiplication
proc `*`*[N: static[int]; T: SomeFloat](x: T; y: Dual[N, T]): Dual[N, T] =
  result.real = x * y.real
  for i in 0..N-1:
   result.dual[i] = x * y.dual[i]

# non-scalar multiplication
proc `*`*[N: static[int]; T: Dual](x: T; y: Dual[N, T]): Dual[N, T] =
  # implementation here
  discard


Run


Re: Passing a sequence by reference to a data type

2020-06-22 Thread ameerwasi
Thanks, guys, I really appreciate your help, this community, and the welcoming 
nature of this forum.


Dual number type

2020-06-22 Thread whiterock
I am trying to implement a dual number type in NIM, but struggling with with, 
for me, obscure error messages. Most likely I am missing something trivial, but 
after having poured in more than 2 hours in trying stuff and researching I 
still could not figure it out. Here's the minimum not working example:


type
  Dual*[N: static[int], T: SomeFloat | Dual] = object
real*: T
dual*: array[N, T]

proc dual*[N: static[int]; T: SomeFloat | Dual](real: T, dual: array[N, 
T]): Dual[N, T] =
  result.real = real
  result.dual = dual

# scalar multiplication
proc `*`*[N: static[int]; T: SomeFloat | Dual](x: SomeFloat; y: Dual[N, 
T]): Dual[N, T] =
  result.real = x * y.real
  for i in 0..N-1:
   result.dual[i] = x * y.dual[i]


Run

Testing this gives different errors which I put in the comments above the 
relevant lines of code. 


# Test
#let a = dual[2, Dual[2, float]](
#  dual[2, float](1.0, [0.0, 0.0]),
#  [dual[2, float](0.0, [0.0, 0.0]), dual[2, float](1.0, [0.0, 0.0])]
#)

let a = dual(3.0, [1.0, 0.0])
echo a # works as expected

# Error: cannot instantiate 'Dual[N, T: float or float32 or float64 or 
Dual]' inside of type definition: '*'; Maybe generic arguments are missing?
echo 183.0*a

# Error: cannot instantiate: '*[2, float]'
echo `*`[2, float](183.0, a)


Run

Thanks in advance, ~whiterock


Re: How to get & set text in clipboard ?

2020-06-22 Thread Trustable
You could have a look at the WinAPI clipboard implementation of NiGui: 
[https://github.com/trustable-code/NiGui/blob/master/src/nigui/private/windows/platform_impl.nim#L421](https://github.com/trustable-code/NiGui/blob/master/src/nigui/private/windows/platform_impl.nim#L421)


Re: First take: parameters

2020-06-22 Thread neo
Thanks, also another solution from the tutorial 
[advanced-types-varargs](https://nim-lang.org/docs/tut1.html#advanced-types-varargs)


proc lfecho(data: varargs[string ,`$`]) =
  write(stdout, "\L")
  for s in items(data):
write(stdout, s)

Run


Re: Idea: Nim Online Conference

2020-06-22 Thread Araq
Once again a big "Thanks you" to every speaker, the quality of the talks were 
high throughout and the fact that every speaker was online during the stream 
and replied to questions made it even more enjoyable.

Coming soon: A hackathon? How to organize one?


Sending emails from Nim using SMTP

2020-06-22 Thread alfrednewman
Hallo,

I would like to send emails from Nim using module smtp.

However, I need to include an attachment with the message.

Is it possible? If so, can you pls provide a code snippet to help me out?


detectOs template in for loop

2020-06-22 Thread neo
What's the best solution for the following:


import distros

for distro in Distribution:
  if detectOs(distro): echo distro

Run

Error: undeclared field: 'distro' found 'distro' of kind 'forVar'

Module distros source shows a Distribution.d enum is expected:


template detectOs*(d: untyped): bool =
  ## Distro/OS detection. For convenience the
  ## required ``Distribution.`` qualifier is added to the
  ## enum value.
  detectOsImpl(Distribution.d)

Run

Exporting detectOsImpl would allow:


for distro in Distribution:
  if detectOsImpl(distro): echo distro

Run

Any suggestions?


Re: Idea: Nim Online Conference

2020-06-22 Thread dom96
We shouldn't forget about @miran's awesome work here. Big thanks to him for all 
his work on scheduling, planning and organising this conference. It went 
incredibly smoothly, the bar has been set very high for the next conf :D


Custom HTML elements

2020-06-22 Thread LancerRevX
Is it possible to create custom html elements with Nim?


Re: Can't install the bin tarball of nim 1.2.2.

2020-06-22 Thread Araq
You can use `-d:nimEmulateOverflowChecks` for 1.2.0


Re: Can't install the bin tarball of nim 1.2.2.

2020-06-22 Thread halloleo
Thanks @shashlick. Yep. The GH issue looks like the same problem.

So, this means I cannot install 1.2.2 from the normal tar ball, right?

Therefore I'm back to square one: tar ball 1.2.0. installs, but gives me a 
compile error in the subsequent C compiler chain.