Re: constness of refs & ptrs

2018-12-18 Thread sendell
1 year later, I'm still worried about that missed opportunity of stronger 
immutability guarantees in Nim.

The whole distinction between var and let feels useless without this. When 
looking at a proc signature, I have no way to know if its parameters will be 
modified, unless they are value-types and let parameters. I like the new "func" 
keyword, but we could make it even closer to what a pure-function should be by 
implementing this, and forbidding var params for funcs.

I'm not aware of the implementation issues, but Araq said it should be possible 
thanks to his write-tracking pass. It also feels like something that should be 
introduced before 1.0, since it will break things, so...

...may I push this idea again ? :)


Re: let not always properly copying string?

2018-12-11 Thread sendell
I think the new assignation semantic for strings and seqs is always copy (no 
distinction between let and var). Copies are avoided whenever a move or passing 
a const ref is possible. Can someone confirm this?


Re: Does Nim need package-level visibility?

2018-10-19 Thread sendell
Restrictions about cyclic type defs and the "forwarding requirement" are 
currently not in line with with nim's otherwise great flexibility, IMHO.

A "package-visibility/friendship" feature is probably needed too (but as 
yglukhov said) not as a replacement of aforementioned features :)


Re: Why is my destructor not called?

2018-01-11 Thread sendell
Global scope should work as any scope, and call destructors when ending.

> adianv said: why should it be called ? x is global and gets never out of 
> scope.

The whole point on RAII is to be abble to bind the lifetime of a resource to 
the lifetime of an object, to make sure the resource is released properly. Your 
point is valid if the resource is memory, it might not be for other kinds of 
resources.


Re: Prevent accidental object copies

2017-12-30 Thread sendell
if you're ok with a var object, you can do:


type Person = object
  first, last: string

proc init(self: var Person, first, last: string) =
  self.first = first
  self.last = last

proc `=`*[T](d: var Person; src: Person) {.error.} =
 discard

var person : Person
person.init("John", "Doe")
echo person.first, " ", person.last



Re: Nim versus Julia benchmark comparison

2017-12-15 Thread sendell
-d:release should use LTO whenever available on the underlying compiler if you 
ask me :) Any downside on making this the default behavior?| 


Re: VoxelSpace engine demo

2017-12-01 Thread sendell
It would be interesting to compile it through emscripten to compare the perfs 
with the original version 


constness of refs & ptrs

2017-11-27 Thread sendell
I was going to talk about this on IRC but I figured out a post would be more 
appropriate. This is also my first post on this forum so _hello, 
nim-community_.  I usually don't get involved in such stuff, but since Nim is 
running towards 1.0 and I have huge expectations for it, I'm making an effort. 

I'm a bit disappointed about ptr/ref constness and I wanted to talk about it. 
As far as I understand it, constness in nim is expressed using:

  * let variables
  * "non-var" proc parameters



As long as the variable is a "value-type" (an _object_ in nim-talk) the const 
guarantees applies. The problem is that when using a ref/ptr, the constness 
applies to the ptr and not the pointed object.

* * *

In C++ speak I have the feeling that:


Object const* ptr; //pointer is mutable, pointed object is const
//cannot be expressed in nim AFAIK


is far more usefull, and in general a stronger guarantee, than: 


Object *const ptr; //pointer is const, pointed object is mutable
//equivalent to let ptr/ref, and "non-var" ptr/ref parameters in nim


* * *

Now an example of why it weakens the guarantees while using nim:

Let say I create an int wrapper like this (obviously useless, but it's an 
example), and an associated proc that prints it, without modifying it. The 
second line of this proc is an error and is catched by the compiler.


type IntBox = object
i:int

proc log(box:IntBox)=
echo $box.i
box.i = 2  # DOES NOT compile: box is not var, i'm not allowed to 
modify it.
# Proc signature says it wont modify box: strong guarantee enforced at 
compile time.


I go further into development and discover that my IntBox instances should be 
shared between multiple locations. I need to allocate them on the heap, and 
thus make the following change in type declaration.

_Note that the semantic of my proc is unchanged: it shouldn't modify the box 
it's applied to._


type IntBox = ref object
i:int

proc log(box:IntBox)=
echo $box.i
box.i = 2   # now it DOES compile, and proc is allowed to modify box!
# Strong guarantee is gone :(


* * *

I'm not sure why things are that way and there's probably a good reason for it. 
I bet on consistency, as ptr/ref are values themselves, and it seems logical 
that constness applies to the same level than for other values.

But still, nim's design is about practicality more than consistency (am I 
wrong?), and here I think we loose a huge practical guarantee.

I'm not sure about the solution, but could we imagine that a ptr/ref constness 
expands to its pointed value? I'm still a newcomer in nim and might be 
mistaken, but I wanted to talk about it.