Re: Changing chained attributes of object taken by var - Is this desired behaviour?

2017-08-06 Thread PeterSR
I must have been tired when I wrote this code. I am pretty certain that I 
understand how the indirection works now. I just remember thinking that it was 
super weird that I could change something and then another variable suddenly 
changed its contents. But of course they pointed to the same thing behind the 
scenes.

Sorry for the inconvenience and thank you for the reply!


Re: Working with ZIP files

2017-08-06 Thread Araq
Can we get these fixes/improvements as a PR please?


Re: Working with ZIP files

2017-08-06 Thread Nox
Thanks, it seems to work fine, though I've already modified libzip to suit my 
needs  It has one advantage over miniz that you can replace existing files in 
ZIP without unpacking everything.

One downside of libzip is that precompiled binary that I have is linked to 
zlib1.dll and I guess there is no way around it without recompiling libzip 
itself.


Re: Changing chained attributes of object taken by var - Is this desired behaviour?

2017-08-06 Thread Araq
Looks correct to me, where is your misunderstanding? You can argue that in 
`takeAsConst` `curr` becomes a stale pointer.


Changing chained attributes of object taken by var - Is this desired behaviour?

2017-08-06 Thread PeterSR
Consider the following code to create linked list-like structure:


type
  A = ref AObj
  AObj = object
id: int
parent: A
child:  A

proc `$`(a: A): string = $a.id


proc takeAsVar(curr: var A, next: A) =
  if curr == nil:
curr = next # var A is wanted here and nowhere else
  else:
let const_curr = curr
echo "takeAsVar: curr before: ", curr
curr.parent.child = next # Try changing curr to const_curr for same 
result
echo "takeAsVar: curr after:  ", curr
# curr changes even though there is no "curr = ..." line.

proc takeAsConst(curr: A, next: A) =
  if curr == nil:
#curr = next # can't do this
discard
  else:
echo "takeAsConst: curr before: ", curr
curr.parent.child = next
echo "takeAsConst: curr after:  ", curr


let a1 = A(id: 1, parent: nil, child: nil)
let a2 = A(id: 2, parent: nil, child: nil)
let a3 = A(id: 3, parent: nil, child: nil)

a1.child  = a2
a2.parent = a1

takeAsVar(a1.child, a3)


let a4 = A(id: 4, parent: nil, child: nil)
let a5 = A(id: 5, parent: nil, child: nil)
let a6 = A(id: 6, parent: nil, child: nil)

a4.child  = a5
a5.parent = a4

takeAsConst(a4.child, a6) # Works as expected


which outputs


takeAsVar: curr before: 2
takeAsVar: curr after:  3
takeAsConst: curr before: 5
takeAsConst: curr after:  5


Notice how the value of the variable curr changes in the first proc without an 
explicit curr = 

We have a very simple object with a parent pointer and a child pointer. My 
initial goal was to create a linked list-like structure and inserting into a 
nil list could be handled by taking the original list by var since it allows 
for curr = next. Most of the logic for non-nil list has been removed to get a 
minimum working example, but the problem is this: When I want to change the 
child field of the parent of curr, then the variable curr itself is changed 
when taken by var.

In my mind taking a parameter by var would only change whether you can write 
curr = ... and change the original or not. Here it seems that it somehow infers 
that curr.parent.child is the same as curr and changes curr as well.

Have I misunderstood how var parameters work? Is this desired behaviour?

Using version 0.16.1 (git hash: 137b5f43022c3c5e22a7f28c8012f1d0ea6007c6)


Re: Nim in Action is finally here

2017-08-06 Thread moigagoo
> Debugging Nim Programs and Using Nim on Microcontrollers

OMG, so cool! I feel like a five-year-old waiting for Christmas 


Re: casting address of pointer functions to a type

2017-08-06 Thread Araq
Your type `PFN_vkCreateDebugReportCallbackEXT` must have a calling convention 
like `stdcall` or `cdecl` for this to work. The default is `closure` which is 
actually a (fn, env) pair.


casting address of pointer functions to a type

2017-08-06 Thread Kerp
Hi again just need to know what i'm doing wrong on this subject

But first here is my code


var vkCreateDebugReportCallbackEXT = 
cast[PFN_vkCreateDebugReportCallbackEXT](vkGetInstanceProcAddr(inst,"vkCreateDebugReportCallbackExt"))



i get a message from the compiler about this when i try it, that it cannot 
convert to a pointer type. i believe its because its pointer function in c and 
maybe nim does not like casting those, i dunno?


Re: Any method to set backend by configuration or nim code?

2017-08-06 Thread Libman
With a sufficiently ugly hack, all things are possible... 


import strutils

static:
  
  proc getNimCompilerCmdLine(): string =
return staticRead("/proc/self/cmdline").replace('\0', ' ')
  
  const nimCompilerCmdLine = getNimCompilerCmdLine()
  
  proc getNimCompilerBackend(): string =
result = nimCompilerCmdLine.split(' ')[1].toLowerAscii()
if result == "compile": result = "c"
  
  proc forceNimCompilerBackend(newBackend: string) =
let currentBackend = getNimCompilerBackend()
if currentBackend == newBackend.toLowerAscii(): return
var splitRestartCmdLine = nimCompilerCmdLine.split(' ')
splitRestartCmdLine[1] = newBackend & " --forceBuild"
let restartCmdLine = splitRestartCmdLine.join(" ")
echo "Restarting the compiler with: `" & restartCmdLine & "`"
echo '{'
let restartEx = gorgeEx(restartCmdLine)
echo restartEx.output
echo '}'
quit restartEx.exitCode
  
  forceNimCompilerBackend("js")


echo "This is printed at run-time."




Re: What can Nim learn from Crystal

2017-08-06 Thread bpr
@Araq

> Which is at least one way too many...

Drop `method`.

@Honhon

I agree that the Nim JS backend is interesting, but I would not try to argue 
for using it in our SW stack. Even OCaml is more of a contender in that space 
since [Reason](https://reasonml.github.io/) .

I also agree that Kotlin got a huge boost because of Google backing it. On the 
JVM, I still favor Scala, which now has a solid JS backend and an up-and-coming 
LLVM backend. I don't code for Android so Kotlin doesn't interest me much yet. 
A compiled Scala with user value types interests me a lot. Scala's slow compile 
times do not 


Re: What can Nim learn from Crystal

2017-08-06 Thread honhon
Agree with BPR mostly.

For me OOP is not really a big deal compared to V1.0 and in that regard Crystal 
has the same drawback as Nim.

I think the obvious trade off with the more aggressive type inference is the 
much slower Crystal compiler. Also you can do things like this in Crystal which 
I don't like:


a = "hello"
a = 1


I've tried Crystal and I mostly enjoyed it.

Positives have been mentioned about Crystal in this thread I think the main 
positives for Nim are, has much better FFI, much more powerful macro system, 
better package manager, better control over the compiler, probably better stack 
trace, and probably better tooling. I didn't mention javascript backend because 
Typescript/Flowtype/Clojurescript/Scala.Js/Purescript/Bucklescript etc have far 
better tooling than Nim so I personally don't see Nim as attractive for doing 
javascript target stuff.

I did feel the Crystal documentation is better organised, more extensive, and 
easier to consume.

V1.0 for Crystal is promised at the end of the year but I'm fairly doubtful but 
its nice they have a roadmap with goals. But basically has the same major 
drawback as Nim at the moment which is no V1.0.

Kotlin will probably be a big mover now with Google backing it in Android and 
also that it will have LLVM backend soon. The IDE is great for Kotlin. The 
compile times are poor like Crystals. Its something you cannot ignore because 
compile time was a big part of Go's success. Nim tells a pretty good story in 
this regard.


Nim in Action is finally here

2017-08-06 Thread dom96
Hey guys!

As you may have already seen Nim in Action is now completely finished. I'd like 
to thank all of you for your patience and kind words throughout the development 
of this book. I'm excited to receive my copies of the book even though I must 
have read it at least 5 times by now. Shouldn't be too long until you have it 
in your hands as well.

P.S. I created a little [promotional website](https://book.picheta.me) where I 
will eventually post some videos and articles about Nim. In particular articles 
for topics that didn't make it into the book like Debugging Nim Programs and 
Using Nim on Microcontrollers.

Hope you guys enjoy the finished product