Re: Variables alignment

2016-08-28 Thread Krux02
As far as I know compilers always align your variables unless you specify that 
you have other priorities, for example with the [packed 
pragma](http://forum.nim-lang.org///nim-lang.org/docs/manual.html#foreign-function-interface-packed-pragma).
 For stack variables I don't know it, but I also don't see any reason for 
compilers to not align variables.


Re: Variables alignment

2016-08-28 Thread sheosi
Krux02, from what I've read about lockfree (namely Jeff Preshing's blog: 
[http://preshing.com](http://forum.nim-lang.org///preshing.com)/ ), one 
requirement for atomics in x86 (and other architectures) are aligned variables. 
And about the types anything from 1 to four bytes.

Araq,I have already tried that, my problem is that I can't use it inside 
objects nor tuples, and while I've yet to try I think I can't use it on 
typedefs neither. I know MSVC always align,but how do I know gcc/clang may 
align them, from what I've tested gcc aligns global variables. 


Re: zeroMem can't use?

2016-08-28 Thread stisa
Nim has implicit initialization ( 
[http://nim-lang.org/docs/manual.html#statements-and-expressions-var-statement](http://forum.nim-lang.org///nim-lang.org/docs/manual.html#statements-and-expressions-var-statement)
 ) so: 


var x : array[10,char]
echo repr x # ['\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0']


Anyway, `alloc0` and the like return pointers, so you need to cast to `ptr 
array[]` :


var inputs = cast[ptr array[10,int]](alloc0(sizeof(int) * 10))
inputs[0] = 1
inputs[1] = 2
inputs[2] = 3
for i in 0..9:
  echo inputs[i]
echo repr inputs

var test = alloc0(10)
zeroMem(test, 10)
echo repr cast[ptr array[10,char]](test)


Produces


1
2
3
0
0
0
0
0
0
0
ref 0018F050 --> [1, 2, 3, 0, 0, 0, 0, 0, 0, 0]
ref 00191050 --> ['\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', 
'\0', '\0']


Or just use create: 


var ar = create array[10,char]
echo repr ar # ref 0092F050 --> ['\0', '\0', '\0', '\0', '\0', 
'\0', '\0', '\0', '\0', '\0']


Note that I don't think nim's garbage collector keeps track of pointers you get 
from `alloc` etc, so you may have to deallocate them yourself to avoid memory 
leaks


Re: Reflections on Nim and using it at work

2016-08-28 Thread bogen
Well, strictly speaking, these are not Posix.

>From the Linux man page: 


ATTRIBUTES
   For an explanation of the terms used in this section, see 
attributes(7).
   
   ┌─┬───┬┐
   │Interface│ Attribute │ Value  │
   ├─┼───┼┤
   │forkpty(), openpty() │ Thread safety │ MT-Safe locale │
   ├─┼───┼┤
   │login_tty()  │ Thread safety │ MT-Unsafe race:ttyname │
   └─┴───┴┘

CONFORMING TO
   These are BSD functions, present in glibc.  They are not 
standardized in POSIX.


So, not sure if the posix module is the correct place to add these, even though 
they are present in glibc on linux.

I tried musl as well, with it the **{.passL: "-lutil".}** is no longer needed. 


Re: Reflections on Nim and using it at work

2016-08-28 Thread bogen
Alright, this is what I got working for the posix stuff: 


{.passL: "-lutil".}

var SIGWINCH* {.importc, header: "".}: cint
var TIOCGWINSZ* {.importc, header: "".}: cuint
var TIOCSWINSZ* {.importc, header: "".}: cuint

proc openpty (amaster, aslave : ptr int, mustBeNil : pointer, termp, winp : 
ptr Termios) : cint {.importc, noDecl, header: "".}
proc forkpty (amaster: ptr int, mustBeNil : pointer, termp, winp : ptr 
Termios) : cint {.importc, noDecl, header: "".}


I need to the put what will make **{.passL: "-lutil".}** happen in the importc 
pragmas for openpty and forkpty, but I'm not sure how to do that. dynlib did 
not do it.

With the above I was able to port the remaining C functions to Nim.

As far as **SigVal** not having **sival_int** an only having **sival_ptr**, 
here was my workaround:


# for inserting:
var si_value:SigVal
si_value.sival_ptr = cast[pointer](myvar)
let rc = pid.sigqueue (SIGUSR2, si_value)

# for extracting:
proc handler (signal:cint, si: var SigInfo, context:pointer) {.noconv.} =
let myvar = cast[mytype](si.si_value.sival_ptr)


Where **mytype** objects are pointer sized or smaller, in my case I'm using an 
enum type. I guess because of that, I don't really see the need to support 
sival_int directly in Nim.

Should something like this be included in the PR?

Here is what is says currently:


SigVal* {.importc: "union sigval",
 header: "", final, pure.} = object ## struct sigval
sival_ptr*: pointer ## pointer signal value;
## integer signal value not defined!


On a different note, why were these renamed?

  * **cfmakeraw** to _cfMakeRaw_
  * **tcsetattr** to _tcSetAttr_
  * **tcgetattr** to _tcGetAttr_



Due to case insensitivity they worked without the camelcase. The reason I 
initially thought they were missing was I could not find the in the 
documentation.

readlink was not renamed to readLink, setgid to setGid, etc, so I don't really 
see a point in renaming the aforementioned.

So, when I do a PR, I'll need to add the proper docstrings, for the new ones 
above as well. 


zeroMem can't use?

2016-08-28 Thread Angluca
win10, nim0.14.2 


# use alloc0
var inputs = cast[array[10,int]](alloc0(sizeof(int) * 10))
inputs[0] = 1
inputs[1] = 2
inputs[2] = 3
for i in 0..9:
  echo inputs[i]
echo repr inputs

var test = alloc0(10)
zeroMem(test, 10)
echo repr cast[array[10,char]](test)



1
2
3
6553000
0
6552640
6553000
1
7607136
1
[1, 2, 3, 6553000, 0, 6552640, 6553000, 1, 7607136, 1]

['P', '@', '\25', '\0', '\0', '\0', '\0', '\0', '\9', '\0']


Maybe I use the incorrect way?


Re: Improved Complex number string representation

2016-08-28 Thread mihirparadkar
Every language with complex number support seems to have its own representation 
of them.

Python does 


 1 + 2j

R does 


 0+1i

Julia does 


 0.0 + 1.0im

Nim seems to follow the same convention as C++ std::complex with 


(0.0, 1.0)

I don't know if one of those is inherently any better than another.


var parameter

2016-08-28 Thread h42
Can I assume that a var parameter will always be a pointer where I can use 
(addr varParm) as copymem destination. It seems to work but I can copy to 
tempory local variable and use normal assignment if there is no guarentee it 
will always work.


Re: Send data structures between threads?

2016-08-28 Thread Varriount
@Araq This sounds like object mapping, in which data from a database is mapped 
into a set of objects. That's quite common, and makes working with data much 
easier.


Re: Send data structures between threads?

2016-08-28 Thread Araq
> So what do you do: do you tell these teams not to use refs? Or do you tell 
> them to go ahead and use refs and garbage collection, and deal with the 
> consequences?

To be honest I would fire teams who model business taxonomies with C++ classes 
(OMG), keeping all the data in RAM (OMG) and not using a (noSQL / SQL) database 
(OMG).

But in the context of Nim, I would likely live with refs, thread local GCs and 
when somebody queries my data, I would return a copy. Note that copying a query 
result doesn't mean to "keep full copies of the data around in RAM" which you 
sort of implied.


Re: Variables alignment

2016-08-28 Thread Araq
Use the `codegenDecl` pragma and to generate any kind of gcc/clang annotation 
you need. Compilers are usually good at alignment though.


Re: GTK 3.20 Nim wrapper

2016-08-28 Thread Stefan_Salewski
Thanks for reporting.

I guess you are refering to the chess game?

Lets look at line 70:


if p0 != p1: window(widget.toplevel).title= "invalid move, ignored."

As the compiler told us, there is a symbol window defined in module gtk3 as 
well as gdk3. So only two possibilities to try. But of course member field 
"title" told us that this is the gtk window, so fix is


if p0 != p1: gtk3.window(widget.toplevel).title= "invalid move, ignored."

You have to do 3 more similar fixes, sorry.

Another fix would be to modify module import list like


import gdk3 exept window

Will fix file at github tomorrow -- I wonder why this error has not occured 
before...

And yes, for that window() cast a proc would be fine, no template necessary. 
But it is some work to fix all that...

If you are on Linux you may test the editor as well, it is now available in v 
0.3. But you have to install nimsuggest first.


Re: GTK 3.20 Nim wrapper

2016-08-28 Thread Simon
I get an error when trying to compile 


board.nim(70, 26) Error: ambiguous call; both gtk3.window(obj: expr) and 
gdk3.window(object: expr) match for: (Widget)


How to fix it?


Re: Send data structures between threads?

2016-08-28 Thread jyelon
I used the word "database" in the generic sense.

Basically, go to any good shopping website (eg, wayfair, or amazon, or google 
shopping, for example), and see what that website "knows" about coffee tables. 
If you take a little time and study the website, I think you'll realize that 
it's a surprisingly large amount of knowledge. There's a whole expert system in 
there about coffee tables - and another expert system about bedspreads, and so 
forth.

At the very base of it all, is a taxonomy:


Home Goods >
  Tools >
Drills and Rotary Tools


Some product categories fit into the taxonomy in multiple places:


Apparel >
  Shirts >
Football Jerseys

Sports >
  Sports Memorabilia >
Football Jerseys


Then, for each category of products, you have data about which products in the 
category are most popular, data to drive a machine-learning classifier that 
classifies queries to determine if they belong in that category, etcetera, 
etcetera. Just take a little time studying one of these websites, and I think 
you'll come to appreciate how much is there.

So when I say a "database" here, I don't mean that it's SQL. I'm just saying 
it's complicated data. It's plain old C++ classes, loaded into RAM - but it's 
legitimately _lots_ of C++ classes, and it takes up _lots_ of RAM.

So imagine, now, that you've got a dozen teams: one is responsible for 
implementing a machine-learning classifier that differentiates queries about 
drills from queries about rotary hammers. Another is building a software system 
whose job is to estimate the popularity of products according to a dozen 
different metrics. Another is building a recommendation system that uses 
statistics to recommend "related products." And so forth.

Now, you know that at some point, you're going to need to integrate all this 
software into a web-server, which will probably be multithreaded.

So what do you do: do you tell these teams not to use refs? Or do you tell them 
to go ahead and use refs and garbage collection, and deal with the consequences?