Re: "Nim for Python Programmers" wiki page

2020-07-11 Thread Stefan_Salewski
> I would like to see this: "Object variants as lightweight alternative to 
> inheritance" explained. :)

Object variants are not really a lightweight alternative to inheritance.

Both concepts are explained in various tutorials, I tried

[http://ssalewski.de/nimprogramming.html#_object_orientated_programming_and_inheritance](http://ssalewski.de/nimprogramming.html#_object_orientated_programming_and_inheritance)

[http://ssalewski.de/nimprogramming.html#_object_variants](http://ssalewski.de/nimprogramming.html#_object_variants)


Re: using nimble for package management

2020-07-11 Thread Stefan_Salewski
> not quite sure what you are asking?

It would be nice to get informed when a new version is available, and it would 
be nice to be able to update all packages, or a subset when updates are 
available.

Generally I do: nimble refresh nimble unistall gintro nimble install gintro

I do not want to collect all the old releases on my harddisk.

Nimble has still an other issue, examples and docs are not provided, so user 
generally additional has to use some git commands to install full git package.

Note that there is a new package manager available created by a core dev in 
IRC, I think it is called nimpf.


Re: using nimble for package management

2020-07-11 Thread Stefan_Salewski
> not quite sure what you are asking?

It would be nice to get informed when a new version is available, and it would 
be nice to be able to update all packages, or a subset when updates are 
available.

Generally I do: nimble refresh nimble unistall gintro nimble install gintro

I do not want to collect all the old releases on my harddisk.

Nimble has still an other issue, examples and docs are not provided, so user 
generally additional has to use some git commands to install full git package.

Note that there is a new package manager available created by a core dev in 
IRC, I think it is called nimpf.


Re: [offtopic]2 cross-platform GUI library

2020-07-11 Thread Stefan_Salewski
Thanks for that hints. I have recently listed available Nim GUI libs, I will 
add these 3 to the list when we can provide working bindings and some examples.

Do you know which on my list are currently working? The list is just above 
Chapter 1 headline in 
[http://ssalewski.de/gtkprogramming.html#_chapter_1](http://ssalewski.de/gtkprogramming.html#_chapter_1).


Re: How to set up/start a Project?

2020-07-07 Thread Stefan_Salewski
Have you tried reading some tutorials?

[https://nim-lang.org/learn.html](https://nim-lang.org/learn.html)

There are also videos at youtube.

Generally in Nim we do not but all objects in its own source file as it is 
common in Java. So generally You just create one single text file for your 
code, and you import modules like strutils, sequitils, tables or whatever you 
need. Many stuff is already available without import from system module. For 
libraries not directly available in Nim we have external packages and the 
nimble or nimph package manager. Of course when your own code grows it may make 
sense to divide it in multiple modules. For a chess game you may have your 
module engine for the chess algorithm, and your module board for the display. 
Maybe one more for saving games to disk.

The official tutorial has section about modules:

[https://nim-lang.org/docs/tut1.html#modules](https://nim-lang.org/docs/tut1.html#modules)

I have also written something about modules:

[http://ssalewski.de/nimprogramming.html#_modules](http://ssalewski.de/nimprogramming.html#_modules)

The other tutorials may contain more details.


Re: NvP: creating and sorting a large array of tuples

2020-07-06 Thread Stefan_Salewski
Yes, that is true. But can you give us some examples for large data blocks that 
have a fixed size known at compile time? I can remember only one, an array of 
floats with a size which had to be a power of two, so we could do a fast 
fourier transform. That was long time ago at university, where we still had an 
old windows 3.1 PC in the lab. Program was in C, I thing watson C compiler, I 
am not sure if it was a dynamically allocated array, or a plain global array.


Re: NvP: creating and sorting a large array of tuples

2020-07-06 Thread Stefan_Salewski
> so that if an array blows through the stack and has to be moved to the heap

Note that array references are rarely used in real life, as there is no benefit 
over sequences. A seq is an object on the stack with contains a pointer to the 
actual data, and a array ref is a pointer on the stack that points to the data 
elements. So performance should be identical. Even performance advantage of a 
plain array to a seq should be not big -- seq has one indirection, but note 
when we work with the seq data, for example sorting it, then we load the start 
address of the data once in a register and never care again about details. 
Mratsim did a performance comparison array vs seq once, there was no difference.

I just noted that you are the Mr Wilcoxson who did the grammar fixes for my 
book, thanks again. But I have the feeling that I still have to explain some 
details better, maybe next winter, currently I am working on a GTK4 book. 


Re: NvP: creating and sorting a large array of tuples

2020-07-04 Thread Stefan_Salewski
> I would have liked to have been able to write:

What exactly was your problem?

The array? Well an array in Nim is a value object, it lives on the stack, so 
there is no pointer indirection involved. But for large arrays the default 
stack size can be too small. In that case we generally use sequences. Use of 
sequences are explained in all the fine tutorials, for performance you should 
create them with right size and avoid add(), use [] instead. My explanation was 
[http://ssalewski.de/nimprogramming.html#_arrays_and_sequences](http://ssalewski.de/nimprogramming.html#_arrays_and_sequences),
 let me know what is unclear or about wrong grammar, I may continue in winter.


Re: Why does wrapping the code in a top level procedure make it faster?

2020-07-03 Thread Stefan_Salewski
> The idea of defaulting to an optimizing mode has come up before.

Sorry, maybe I made my statement not clear enough: Of course it is fine that 
default compiler mode is to use runtime checks and do not use -d:danger. 
@didlybom asked to generate an automatic main function so that newcomers making 
a benchmark would get good results. But generally these newcomers also forget 
to compile with -d:release, and also these newcomers often use too many ref 
objects, too many string allocations and other slow stuff.

So the recommendation is: Before making benchmarks or videos read a tutorial. 


Re: Why does wrapping the code in a top level procedure make it faster?

2020-07-03 Thread Stefan_Salewski
> whenever somebody does a benchmark

So should we make -d:danger the default just for the case a fool makes a 
benchmark?

Global code is generally used only for short scripts where nobody really cares 
for performance. And the benefit of a main proc is explained in most tutorials, 
it is in section 
[http://ssalewski.de/nimprogramming.html#_scoping_visibility_and_locality](http://ssalewski.de/nimprogramming.html#_scoping_visibility_and_locality)
 in my book.


Re: How to use inheritance?

2020-06-27 Thread Stefan_Salewski
And finally this may look more like what you may desire:


import gintro/[gtk, gobject, gio]

type
  Header = ref object of Headerbar
text: string
  
  MainWindow = ref object of gtk.ApplicationWindow
header: Header
pb: ProgressBar

proc appActivate(app: Application) =
  let window = newApplicationWindow(MainWindow, app)
  window.borderWidth = 10
  window.header = newHeaderbar(Header)
  window.header.setShowCloseButton
  window.header.setTitle("MainWindow")
  window.header.setSubTitle("Subtitle")
  window.pb = newProgressBar()
  window.add(window.pb)
  window.pb.setFraction(0.25)
  window.defaultSize = (400, 400)
  window.setTitlebar(window.header)
  window.showAll()

proc main =
  let app = newApplication("org.gtk.example")
  connect(app, "activate", appActivate)
  discard run(app)

main()


Run

Please note that you should add destructors for the subclassed widgets if you 
intent to compile with ARC, see the gintro README.


Re: How to use inheritance?

2020-06-27 Thread Stefan_Salewski
And to give you a starting point, the basic shape of your above program would 
look like this:


import gintro/[gtk, gobject, gio]

type
  Header = ref object of Headerbar
text: string
  
  MainWindow = ref object of gtk.ApplicationWindow
header: Header
pb: ProgressBar

proc newHeader(): Header =
  result = newHeaderbar(Header)
  result.text = "Initial Title"
  result.setTitle("Title")
  discard result.showCloseButton

proc newMainWindow(gtkApplication: Application): MainWindow =
  result = newApplicationWindow(MainWindow, gtkApplication)
  result.header = newHeader()
  result.pb = newProgressBar()
  result.add(result.pb)
  result.pb.setFraction(0.25)
  result.setSizeRequest(400, 400)
  result.setTitlebar(result.header)
  result.showAll()

proc appActivate(app: Application) =
  let window = newMainWindow(app)

proc main =
  let app = newApplication("org.gtk.example")
  connect(app, "activate", appActivate)
  discard run(app)

main()


Run

For me it compiles and runs, but I think it does not look like you intend. I 
can not really test it as my current gintro is modified for my GTK4 
experiments. Hopy you can tune it yourself.


Re: How to use inheritance?

2020-06-27 Thread Stefan_Salewski
OK, when it is gintro related then your initial post is really not that smart.

Inheritance for C libs in different from inheritance in Nim . C is not an OOP 
language.

I would strongly suggest YOU not using gintro!

I have recently listed most of the Nim GUI packages here, located directly 
above Chapter 1 heading:

[http://ssalewski.de/gtkprogramming.html](http://ssalewski.de/gtkprogramming.html)

All that packages are great. If I miss other packages please let me know.

If you will still continue with gintro, I gave an example of subclassing here:

[http://ssalewski.de/gintroreadme.html#_extending_or_sub_classing_widgets](http://ssalewski.de/gintroreadme.html#_extending_or_sub_classing_widgets)

It is not well tested, and it is different from native Nim and different from 
GObject C subclassing. As we use proxy objects in gintro.

Nim inheritance is explained here:

[http://ssalewski.de/nimprogramming.html#_object_orientated_programming_and_inheritance](http://ssalewski.de/nimprogramming.html#_object_orientated_programming_and_inheritance)


Re: How to use inheritance?

2020-06-27 Thread Stefan_Salewski
I hope it is not related to gintro?


Re: Naming conventions - need leading underscore

2020-06-24 Thread Stefan_Salewski
> How do you solve this problem

First, I can not remember that I myself cared for the fact if a symbol in 
source code I was reading or working on was local/private or public. There may 
exists cases when one cares, but I just can not remember a concrete case.

Second, we can use unicode for symbol names. I never did, but maybe there is a 
nice unicode symbol available?

And further, when a leading underscore should indicate private symbols, then 
the compiler had to enforce it, so that the visible appearance is always 
correct. And when we use leading underscore as private mark, then we can also 
use question mark to indicate query as sorted?() in Ruby and Chrystal to return 
a bool instead a sorted copy. And then we can ask for more such visible markers.

And finally we may have IDE/Editor support to differentiate between private and 
public symbols.


Re: Passing a sequence by reference to a data type

2020-06-21 Thread Stefan_Salewski
Yes, SolitudeSF is right:


type
  lis = ref object of RootObj
v : ref seq[int]

var s : ref seq[int] = new seq[int]
s[] = @[1, 2, 3, 4, 5]
let list = lis()
list.v = s
echo list.v[]
s[].add(13)
echo list.v[]


Run

We have to call new explizit in this case. And I was a bit surprised that we 
need actually the subscript operator in the second last line, as often the 
dereference operation is not needed in Nim.


Re: Passing a sequence by reference to a data type

2020-06-20 Thread Stefan_Salewski
I have no idea what shallowCopy(list.v, s) should do in this case.

Your v : seq[int] and your let s = @[1, 2, 3, 4, 5] both defines value types. 
Well v is a field of a reference to an object, but v is still seq, and seq are 
value types in Nim, when v get filled with data v allocates its own data buffer 
and stores its values there. I would guess what you want is v : ref seq[int]. 
Maybe with


#let s = @[1, 2, 3, 4, 5]
var s = ref seq[int]
s[].add(1) # or s[] = @[1, 2, 3, 4, 5]
list.v = s


Run


Re: How to get & set text in clipboard ?

2020-06-20 Thread Stefan_Salewski
https://forum.nim-lang.org/t/4820#30170


Re: Perf: Table.del(key)is taking 85% of my code's time

2020-06-18 Thread Stefan_Salewski
> key distribution matters a lot

That is true, but it is obviously not the problem for Mr snej. He gets good 
performance when he disables all checks one by one, but not with -d:danger. So 
his configuration is somewhat broken.

And note, -d:danger is enough, no need for -d:danger -d:release. We should not 
confuse people too much. Same for -d:release --opt:speed, -d:release already 
includes --opt:speed.


Re: Hi all, pass Nim functions to C code as callbacks?

2020-06-18 Thread Stefan_Salewski
That is done in libs like gtk a lot. The Nim procs needs the cdecl pragma for 
this to work.


Re: Perf: Table.del(key)is taking 85% of my code's time

2020-06-17 Thread Stefan_Salewski
> use binary size as a rough indicator of optimization

That makes not much sense. For gcc -O3 gives generally large but fast 
executables. Large because gcc unrolls loops and apply SIMD instructions and 
much more.

Have you tested -d:danger ? That option should really give you fastest code and 
turn of all checks. If not then there is something wrong. If -d:danger is fine, 
then you may try without danger and turn of checks to find out what check makes 
it slow. Or create a minimal example for table that we can investigate.


Re: Perf: Table.del(key)is taking 85% of my code's time

2020-06-17 Thread Stefan_Salewski
> and everything got about 20 times faster.

We have never seen such an behaviour before, so it would be great if you could 
investigate it in more detail.

Maybe a strange bug? Did you use gcc10 or clang, or maybe the microsoft or 
intel compiler as backend?

-d:release is fine, -d:danger can be a bit faster as it turns off all runtime 
checks. Both is O3 for gcc by default. But specifying --opt:size at the same 
time may be not that good, do you use -O3 and -Os for gcc at the same time? 
Most people do not.|   
---|---  
  
Try --passC:-flto to for lto, that give you inlining for all procs.

I enable generally -march=native also. And you may try PGO, there was a post 
about it recently on the forum.


Funny, we can cast ptr to var for proc arguments

2020-06-06 Thread Stefan_Salewski
This is fine of course:


proc p1(pi: ptr int) =
  pi[] = 7

proc p2(pi: ptr int) =
  p1(pi)

var x = create(int)
p2(x)
echo x[]


Run

But I did not expect that this would compile:


proc p3(i: var int) =
  i = 13

proc p4(pi: ptr int) =
  p3(cast[var int](pi))

var y = create(int)
p4(y)
echo y[]


Run

On the other hand, var proc parameters are basically pointers, so maybe I 
should be not surprised. That type of casting can be useful for C wrappers, 
when we have a C pointer and want to call a Nim proc with a var parameter...


Re: Justification for auto type

2020-06-04 Thread Stefan_Salewski
The auto keyword is used a few times in the rtree module, see


grep auto RTree/src/rtree.nim
proc center(r: Box): auto = #BoxCenter[r.len, type(r[0].a)] =
proc distance(c1, c2: BoxCenter): auto = # squared!
proc overlap(r1, r2: Box): auto =
proc area(r: Box): auto = #type(r[0].a) =
proc margin(r: Box): auto = #type(r[0].a) =
proc enlargement(r1, r2: Box): auto =
proc bestBinarySplit[M, D: Dim; RT, LT](d: array[TgsX * D, seq[LTGS[D, RT, 
LT]]]; m: int): auto =
  proc rseq_nearest[D, RT](rs: RSeq; n: BoxCenter[D, RT]): auto =



Run

I was not able to get it to compile without that time. But maybe the compiler 
has made some progress now, do you have suggestions for removing it? 


Re: incorrect set-to-int conversion

2020-05-27 Thread Stefan_Salewski
Yes, that is well known, I discussed that behaviour with Araq 3 years ago 
related to gintro. I then added dummy zero flags when the gtk enums started not 
with 0. Note that enums with wholes may become deprecated in future, so we may 
have to add even dummy values for missing values. Araq sometimes recommends 
using distinct ints instead of enums. But then we have no pure enum values like 
TrafficLight.red but have to use tl_red or put the values in a separate module 
just to get a qualifier.


Re: Question about type safety and OOP

2020-05-24 Thread Stefan_Salewski
snej, seq is in modern Nim a value object, not a ref. In old Nimrod we could 
assign nil to a seq, but that is not possible today.

mantielero,


var a:seq[A] = @[B(val:1), B(val:2)]


Run

can not work as Nim is a statically typed language, seq[A] and seq[B] are 
different types, so assignment is not allowed. 


Re: Inheritance vs composition

2020-05-24 Thread Stefan_Salewski
Composition is more flexible than inheritance. You can put many objects into 
another object with composition, while with inheritance you only subclass. Note 
that another option are the sum types, also called ADT or objects variants. 
They can have advantages for performance, as you can have value types in a seq 
then, also mentioned in my book.


Re: Question about type safety and OOP

2020-05-24 Thread Stefan_Salewski
> Is there a way to make it accept all the instances inheriting from Curve?

As that question arises often, I tried to explain that in my book (with 
geometric shapes).

[http://ssalewski.de/nimprogramming.html#_object_orientated_programming_and_inheritance](http://ssalewski.de/nimprogramming.html#_object_orientated_programming_and_inheritance)

It is still not clear enough?


Re: Strange failure

2020-05-24 Thread Stefan_Salewski
Your pt proc is a generic proc, that is when you call it with different types, 
then for each type an instance is created.

For t1 you call it with int arguments, so an instance for ints is created.

For t2 and t3 first argument is a float, so a float instance is created, the 
other literals are passed as float too.

For t4 first arg is a int literal, so the already existing int instance is 
used, but second arg if of type float, which is not automatically converted to 
int, so the compile error.

Hope that is right. 


Re: How to instantiate `ptr object`

2020-05-19 Thread Stefan_Salewski

y: 1.2E3


Run

Thanks for that hint. Of course the book is in an very early state still, I 
wrote most of the two first chapters in April. But I hope and think that it can 
be already useful for beginners with nearly no CS knowledge. I have just 
yesterday applied a lot fixes provided by Jim Wilcoxson, mostly concerning 
English grammar. Current book content is the result of a fast flow directly 
from my head to the keyboard, so examples are untested and book content is not 
compared to the Nim manual in detail still. For reporting small issues you may 
use github at 
[https://github.com/StefanSalewski/NimProgrammingBook](https://github.com/StefanSalewski/NimProgrammingBook),
 or for a general discussion 
[https://forum.nim-lang.org/t/6170#38482](https://forum.nim-lang.org/t/6170#38482).


Re: New blog, with some Nim articles

2020-05-15 Thread Stefan_Salewski
Idiomatic Nim code is generally very fast, and with some knowledge you can 
ALWAYS write Nim code with performance close to other high performance 
languages as C or C++.


Re: New blog, with some Nim articles

2020-05-15 Thread Stefan_Salewski
> the sentence may give a wrong impression.

Indeed. And again I have the perfect wording in my book :-)

"Nim is fast. Generally performance is very close to other high performance 
languages as C or C++. There are some exceptions still — other languages may 
have libraries or applications that are tuned for performance for many years, 
while similar Nim applications are less tuned for performance yet, or maybe are 
more written with a priority of short and clean code or runtime safety."


Re: How to instantiate `ptr object`

2020-05-14 Thread Stefan_Salewski
For your example ARC makes a gigantic difference. (I put your global code in a 
main proc)


import times

type FooBase = ref object {.inheritable.}
  dummy: int

type Foo{.final.} = ref object of FooBase
  value : float32

proc inplace_add_proc(x: var Foo, a: float32) =
  x.value += a

proc inplace_add_closure(x: var float32, a: float32) =
  proc add_closure(v: var float32) = v += a
  add_closure(x)

method inplace_add_method(x: FooBase, a: float32) {.base.} =
  discard

method inplace_add_method(x: Foo, a: float32) =
  x.value += a

proc main =
  
  var bar : Foo
  new bar
  var start = cpuTime()
  for i in 0..<1:
inplace_add_proc(bar, 1.0f)
  echo " Proc with ref object ", cpuTime() - start
  
  var x : float32
  start = cpuTime()
  for i in 0..<1:
inplace_add_closure(x, 1.0f)
  echo " Closures ", cpuTime() - start
  
  var baz : Foo
  new baz
  start = cpuTime()
  for i in 0..<1:
inplace_add_method(baz, 1.0f)
  echo " Methods ", cpuTime() - start

main()


Run


$ nim c -r -d:danger t3.nim
 Proc with ref object 0.117909274
 Closures 1.719076293
 Methods 0.264790297999


Run


$ nim c -r -d:danger --gc:arc t3.nim
 Proc with ref object 0.118106775
 Closures 0.941439717001
 Methods 3.822302076


Run


Re: Game unlock gui written with gintro

2020-05-14 Thread Stefan_Salewski
> Isn't it the official way to bind signals automatically?

I don't know. I read the book of A. Krause in 2007, maybe he mentioned it, but 
I can not remember. And I myself do not use GTK Builder and Glade. I know that 
E. Bassi strongly recommends using GTK Builder, for C coders it may be indeed 
helpful. But I myself have always trouble with Glade, I never found a really 
good tutorial. Some people write or fix the GTK builder XML files manually. 
Maybe in next winter I may try it again. Hopefully Glade will support GTK4 then.


Re: Game unlock gui written with gintro

2020-05-14 Thread Stefan_Salewski
> send you some coffee money

Thanks for the suggestion, but as I spent 1400 hours on gintro and before that 
already 600 hours on oldGTK3 the gain per hour would be in the cent area still. 
So better send the money to a charity organisation.


Re: Game unlock gui written with gintro

2020-05-14 Thread Stefan_Salewski
Great that you have not already retired!

As you seem to be the only active english speaking gintro user beside myself, 
is it OK for you when we remove the old subclassing scheme for next release? 
The new style is


let button = newButton(CountButton, "Counting down from 100 by 5")


Run

As described in 
[http://ssalewski.de/gintroreadme.html#_extending_or_sub_classing_widgets](http://ssalewski.de/gintroreadme.html#_extending_or_sub_classing_widgets)

I think the new style is simpler and generally saves one line of code, and the 
old style was already deprecated in gintro 0.7.3.

Or can you imagine a situation where the old style is preferable?

Next gintro release may come soon, with support for gtk 3.98.3 which will 
become GTK 4 end of this year. 3.98 introduced a few new classes which are not 
gobject based, which generated some additional trouble for the bindings. But 
now the examples compile again, I have still to clean up the generator script.

Your proposed glade signal connect is not yet supported, I was not even sure if 
that was possible at all. I may have read about it in 2007, but can not 
remember currently. Maybe that is a task for next winter.


Re: Idea why this does not compile?

2020-05-13 Thread Stefan_Salewski
> need to define your destructor earlier

Yes, my initial guess was indeed that, as it was done in that way in the 
earlier gintro code.

But I became unsure if it would work really fine also that way.

And it remains strange, as my fnew template should fully hide the use of the 
finalizer when we compile with --gc:arc. So I assume that both branches of the 
when construct are processed by the compiler and that confuses the compiler? In 
the same way as discarded code is always syntax checked?

Unfortunately ordering the code is not that easy, as it is autogenerated from 
the gobject-introspection database, and for latest gtk 3.98.3 which will become 
4.0 there are some serious changes. I can not just put the =destroy just after 
the type declaration, as =destroy calls low level procs like 
gdk_event_unref(self.impl) above. So carefully sorting will be necessary.

Note that forward declarations like below do not compile:


proc gdk_event_unref(self: ptr Event00)

proc gdk_event_unref(self: ptr Event00) {.importc, libprag.}


Run


Error: pragmas are only allowed in the header of a proc; redefinition of 
'gdk_event_unref'


Run

What should work is declaring a special unref function before declaring the 
=destroy and using that one in the =destroy like


proc destroy_gdk_event_unref(self: ptr Event00) {.importc: gdk_event_unref, 
libprag.}


Run


Idea why this does not compile?

2020-05-13 Thread Stefan_Salewski
I was just working a bit on gintro for latest GTK4 and got a really confusing 
error message.

Luckely I was just able to generate a minimal example, but I have still no real 
idea what the problem may be. I guess there must be something special in this 
code, because I have used similar constructs before and it was working fine...


type
  Event* = ref object of RootRef
impl*: pointer
ignoreFinalizer*: bool

template fnew*(a: untyped; finalizer: untyped) =
  when defined(gcDestructors):
new(a)
  else:
new(a, finalizer)

proc `ref`*(self: Event): Event =
  fnew(result, generic_gdk_event_unref)
  echo "result.impl = gdk_event_ref(cast[ptr Event00](self.impl))"

proc generic_gdk_event_unref*[T](self: ref T) =
  if not self.ignoreFinalizer:
echo "gdk_event_unref(self.impl)"

when defined(gcDestructors):
  proc `=destroy`*(self: var typeof(Event()[])) =
if not self.ignoreFinalizer and self.impl != nil:
  echo "gdk_event_unref(self.impl)"
  self.impl = nil

proc main =
  var x: Event
  fnew(x, generic_gdk_event_unref)

main()


Run


$ nim c --gc:arc t2.nim

/tmp/hhh/t2.nim(21, 3) Error: cannot bind another '=destroy' to: 
Event:ObjectType; previous declaration was constructed here implicitly: 
/tmp/hhh/t2.nim(13, 3)



Run


$ nim -v
Nim Compiler Version 1.3.3 [Linux: amd64]
Compiled at 2020-05-11
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 7c24250a575b4d441ba6d7301714cbb246425fb6
active boot switches: -d:release


Run


Re: How to instantiate `ptr object`

2020-05-13 Thread Stefan_Salewski
> you can't mix inheritance with un-managed raw pointers

That is an interesting point, I was wondering about it this morning already 
when I wrote my first reply.

I am still not sure. I could generally imagine that inheritance can work with 
with un-managed raw pointers. But maybe not in Nim?

@dataPulverizer note that create() may be easier to use, as with create() no 
cast is necessary.

@snej The book is in a very early stage still, I wrote the two chapters in 
April. And it is mostly for people with very less programming experience.


Re: Terminal based GINTRO(GTK) VTE

2020-05-12 Thread Stefan_Salewski
> But the best thing is to ask #Stefan_Salewski

Not really as he does not use Windows.

I added VTE to gintro some years ago because someone asked for it, but that guy 
never came back.

I already asked Google about VTE for Windows, but with no clear result.

When people really intent to use VTE on Windows, then we can ask E. Bassi on 
GTK forum. But I hesitate to bother him with questions for which no one really 
cares.


Re: How to instantiate `ptr object`

2020-05-12 Thread Stefan_Salewski
That is also covered in my book in detail. Have you starting reading?

[http://ssalewski.de/nimprogramming.html#_allocating_objects](http://ssalewski.de/nimprogramming.html#_allocating_objects)

alloc(), create(), dealloc().


Re: Reference to sequence

2020-05-10 Thread Stefan_Salewski
> would be a builtin for transforming something to ref,

Have you been able to read

[http://ssalewski.de/nimprogramming.html#_references_and_pointers](http://ssalewski.de/nimprogramming.html#_references_and_pointers)

If so, what may I improve?


The let alias trap with HashSet[string]

2020-05-10 Thread Stefan_Salewski

import sets
var h: HashSet[string]

proc main =
  h.incl("Nim")
  let b = h
  h.incl("VLang")
  h = b
  echo h.contains("VLang")

main()


Run

The output is true.

But with "var b" it would be false. So it seems that "let b = h" creates just 
an alias.

I can now remember a similar case some years ago. But it is too hard to always 
have in mind that the code above with let is only an alias. With strings it 
seems to work like a real copy, and when all code is in global scope it seems 
to work also like a copy.


Nim Compiler Version 1.1.1 [Linux: amd64]
Compiled at 2020-03-26


Run


Re: gintro - glade event problems and multilingualism

2020-05-08 Thread Stefan_Salewski
Fine that it works at all for you.

Unfortunately I can not really help for both questions, and I am busy with 
adapting gintro to latest GTK 3.98.3 which will become GTK4. That adaption is 
some more work than expected, see

[https://discourse.gnome.org/t/recent-gtk-3-98-nim-example-fails-to-compile/3262/9](https://discourse.gnome.org/t/recent-gtk-3-98-nim-example-fails-to-compile/3262/9)

[https://discourse.gnome.org/t/g-arg-info-may-be-null-for-the-instace-itself/3284/6](https://discourse.gnome.org/t/g-arg-info-may-be-null-for-the-instace-itself/3284/6)

For the second question with multilingualism: When that is a Windows problem 
then maybe we should ask on the GTK forum.

For glade and automatically connecting to signals, I intended to investigate 
that for some years already, but never managed to really do it, that is to find 
time and motivation. Indeed we should try to enable that without the need to 
access the low level objects, I will try to investigate that topic soon. 
Personally I do not use Glade and GTK Builder, so I do not know much about both.


Re: Terminal based GINTRO(GTK) VTE

2020-05-08 Thread Stefan_Salewski
> Keep in mind, that VTE is not supported on windows.

Is that an gintro issue or a general GTK restriction? I already asked google, 
but got no clear answer, maybe we have to ask on GTK forum.


Re: Cast float64 to sequence of bytes and cast sequence of bytes to float64

2020-05-07 Thread Stefan_Salewski
See

[http://ssalewski.de/nimprogramming.html#_casts_and_type_conversion](http://ssalewski.de/nimprogramming.html#_casts_and_type_conversion)

first. Then it should be easy to cast a float to an array of 8 bytes and back. 
And when you have a seq containing exactly 8 bytes, then you can copy the 8 
bytes of your array to the seq and back.

So it should become clear that a direct cast between seq and float is not 
possible, as both data structures are very different. The seq has its buffer 
which contains the elements, but also a length and capacity field at least.

I think the only operation what you may do is to cast the buffer of the seq to 
a float, at least you can copy the buffer of the seq to a float byte by byte. 
But you have to take into account that the buffer of the seq may be invalidated 
when you change the seq -- when you grow the seq it may allocate a new buffer 
and old buffer may be freed. Note, the address of the buffer of the seq is the 
addr of the first element, that is element with subscript 0. 


Re: How to use Clang LTO + PGO with Nim

2020-05-06 Thread Stefan_Salewski
That is interesting.

Note that CBlake recently gave instructions for gcc:

[https://forum.nim-lang.org/t/6283#38755](https://forum.nim-lang.org/t/6283#38755)

We really should collect these instructions somewhere, maybe in the wiki. 


Re: Performance issues with "complex" module

2020-05-02 Thread Stefan_Salewski
Yes, your guess that missing inlining is the problem seems to be true.

We generally use link time optimization, which is really good with gcc10. Try


 $ nim c -d:release --passC:-flto t.nim

$ ./t
julia1: 114 ms (sum of pixels: 27677748)
julia2: 115 ms (sum of pixels: 27677748)
julia3: 111 ms (sum of pixels: 27677748)


Run

There are more options to tweak of course, like ARC or march=native and such.


Re: Nim sets compatibility/conversion to C

2020-04-29 Thread Stefan_Salewski
Yes, matching C enums and Nim enums needs some care.

In your case, I have the feeling that RendererFlip should be a Nim set from the 
beginning. As it looks like a bitset which can be empty, bit 0 can be set, or 
bit 1 can be set.

I did that for a few types in gintro.

Generally enums not starting at 0 are a problem in Nim too, I had to add dummy 
0 values in gintro. And I think that enums with holes will vanish in Nim some 
day, I think they are already deprecated. But I think in gintro we had no enums 
with holes. Araq sometimes recommends using distinct int constants instead of 
enums in Nim. But for gintro enums and sets of enums work fine.


Re: Embedding OpenGL window into some of our gui frameworks

2020-04-28 Thread Stefan_Salewski
> do it in other frameworks as well.

I strongly assume that we would be able to do something like

[https://github.com/ebassi/glarea-example](https://github.com/ebassi/glarea-example)

with gintro for gtk3 and gtk4. But I am too lazy to try it. And I assume that 
GTK4 will get Vulkan support some day, maybe better to try that later.


Re: About include

2020-04-27 Thread Stefan_Salewski
You are doing "interesting" things.

First, I can not remember having seen nested templates at all in Nim, at least 
I never used that construct.

And putting a include inside a template is also "interesting".

>From my memory an include should be identical to copying the content at that 
>location. 


Re: NOOB: Few simple questions ...

2020-04-25 Thread Stefan_Salewski
For the first question I have no idea what you mean.

For the second, Nim has no classes, so there are no methods coupled closely to 
objects. But for the fields of objects, there are various fields and fieldPairs 
iterators, see

[https://nim-lang.org/docs/iterators.html#fields.i%2CT](https://nim-lang.org/docs/iterators.html#fields.i%2CT)


Re: import vs include

2020-04-25 Thread Stefan_Salewski
> I suppose that if I import bar.nim in multiple files, I would get types 
> redefinitions.

I don't think so, but please let me know.

And thanks for your example, I have to mention that for objects it can be 
necessary to export fields too.

With imports the really fun part is cyclic imports. We have forum posts about 
that, I have to add that also to the book.

When you really love to write ptr cuint) and cast[ptr cuint](addr 
result.build), I am not sure if Nim is such a good choice :-)


Re: import vs include

2020-04-25 Thread Stefan_Salewski
> I don't understand what import really does. When you use import, then you 
> have to mark the symbols which you want to import by export markers asterisk.

See

[http://ssalewski.de/nimprogramming.html#_modules](http://ssalewski.de/nimprogramming.html#_modules)

[http://ssalewski.de/nimprogramming.html#_include](http://ssalewski.de/nimprogramming.html#_include)


Re: Nim programming book for kids

2020-04-20 Thread Stefan_Salewski
Yes, we intend indeed a Chinese translation.

The problem with translations is always, that after the initial translation it 
is difficult to update or extend a book. So general I recommend today writing 
and reading all in English only. But for such a book for beginners a 
translation may make sense , as young Chinese people may not know English that 
well already. Theoretically I could do a German version myself of course, but 
for 2 or 3 Germans that would not really make sense :-)


Re: Nim programming book for kids

2020-04-19 Thread Stefan_Salewski
> a much better way to teach kids (and many adults) programming is by creating 
> videos

Funny to hear that from someone who wrote a Nim book. Wisdom of the ages or 
more a result of dissapointment by tiny selling rates :-) ?

Personally I can not imagine something more boring and time wasting than 
videos. Most videos are extremly bad, generally I understand what I already 
know, but have trouble learning new things. And what I learn from a video, I 
could have learned in a fraction of the time from a written tutorial or book. I 
think for me one problem is that I have some problems to understand the English 
speech at all, but the other point is that I can read very quickly over text 
that I already know, but think long about new stuff.

Well there exists good videos indeed. Some years ago we had in 3Sat TV a series 
of Prof Manfred Spitzer about our brain, was really good. And in ARD-Alpha 
there was Alpha-Centauri of Prof. Harald Lesch from university of Munich, about 
our cosmos. Was not bad. But youtube videos? Pure garbage, they can not even 
explain simple car repair instructions. 


Re: if error

2020-04-19 Thread Stefan_Salewski
Why do you intent to use option[Cell] at all?

I have never used option types myself, but I would assume that there is some 
overhead involved. Or do you think there is not?

You have the Cell object with x and y fields of type integer. So it should be 
possible to define a x, y combination that is a invalid state, maybe x, y both 
with value int.high. Whenever your proc returns that invalid state you can 
detect it, you can declare your own isNil() or isInvalid() proc for Cell data 
type and check for invalid state. But well, option types may be fun, I would 
assume that they work with hidden tuples? I should read more about them.


Re: Return type

2020-04-19 Thread Stefan_Salewski
@Hlaaftana

> string is compatible with openarray[char]

Interesting. Is that guaranteed and stated in the manual?

@Javi

There may be indeed ways to simplify your source code by using auto keyword or 
by using typeof(param) as type for the result. I think I have used that 
somewhere, will try. But for each different array size that is used a own proc 
is instantiated.


Re: Return type

2020-04-18 Thread Stefan_Salewski
First, openArray stands for array or seq, not for string. Well I hope that is 
still the case, it was when I read the tutorial five years ago. I guess one can 
cast a seq[char] to string.

Note that Nim is statically typed, so the return type is well defined at 
compile time. But array type and seq type is very different, you may read that 
section already in my kids book, but it may contain some errors still.

So you can not have a single proc that can return array or seq. Basically you 
need two different procs.

We have "when" keyword in Nim as a compile time "if", and "auto" keyword as a 
possible return type. Both may help that we have to write only one proc, but 
the compiler will instantiate two for us. We have also or-types in Nim, we can 
write "int or float" in the parameter list and the compiler will instantiate 
one proc with int and one with float for us, and in the proc we can test with 
something like "when typeof(x) is float:"

But arrays are not a type but a typeclass, as arrays with same base type but 
different length are incompatible.

So I think there is no solution for you. Can you tell us what you want to 
archive? 


Re: I think we can really do better...

2020-04-17 Thread Stefan_Salewski
Note, my initial suggestion was

"Nim combines successful concepts from mature language like Python, Ada and 
Modula with a few sounding features of latest research."

Unfortunately they discarded the last part, I think to fit it all to one page.

But I have copied all my advertisings to my book:

[http://ssalewski.de/nimprogramming.html](http://ssalewski.de/nimprogramming.html)

Section

[http://ssalewski.de/nimprogramming.html#_nim_has_a_encouraging_future](http://ssalewski.de/nimprogramming.html#_nim_has_a_encouraging_future)

is maybe too much advertising, maybe I will remove that.

The fact that you do not know Modula and Ada is not that great, Modula was an 
important milestone in development of languages, it was the successor of Pascal 
and introduced the module concept around 1980 which current C++ is still 
missing. And Ada, designed by a committee it may be a bit ugly, but it has been 
improved and is still used in critical areas like aircraft.

All languages built on each other in a way, each new languages has learned from 
previous ones and tries to avoid their mistakes.

I guess even V-Lang will not claim to be something revolutionary new?

And Rust -- it is indeed a better C++, and has a large company behind it and 
much hype. 


Re: Iterate over fields

2020-04-15 Thread Stefan_Salewski
I have never used that iterators myself.

But I would strongly assume that we can only iterate over one object at a time.

As we can only iterate over one string or over one sequence at a time.

But of course we can collect the results of each iteration is some way, and 
finally output all collected data in the desired format. 


Re: Iterate over fields

2020-04-14 Thread Stefan_Salewski
There are iterators fields and fieldPairs:

[https://nim-lang.org/docs/iterators.html#fieldPairs.i%2CT](https://nim-lang.org/docs/iterators.html#fieldPairs.i%2CT)


Re: generate c++ code from .nim file

2020-04-13 Thread Stefan_Salewski
nim cpp mynimfile.nim

The c++ code is stored in the nimcache directory, see

[https://nim-lang.org/docs/nimc.html#compiler-usage-generated-c-code-directory](https://nim-lang.org/docs/nimc.html#compiler-usage-generated-c-code-directory)


Re: Nim programming book for kids

2020-04-13 Thread Stefan_Salewski
No, I think it was a different book. Unfortunately I am not even sure if it was 
written in English or German language. I would guess German, as I was really 
not good at English at that time, but maybe it was written in an very easy form 
of English. It was a thick book, maybe 400 to 500 pages, and I think not 
hard-cover but paperback. One example code that I remember well was a short 
code fragment that calculated the weekday from a date. And the funny fact was 
that the author had to admit that he did not understood how exactly that code 
worked, that code segment was really surprisingly short.

But maybe it is better to do not really search for that book again -- I might 
be dissapointed when reading it again. 


Re: Nim programming book for kids

2020-04-13 Thread Stefan_Salewski
Maybe one additional note: I follow the Nim development now for about 5 years, 
and from time to time fully unskilled people came to the community. Some asked 
if Nim is well suited for beginners. Some Nim people said it is, like Dom, some 
said it is not. I personally recommended Nim only to really bright beginners, 
as learning CS and programming with Nim is not that easy, due to small 
community and due to restricted learning resources. The available learning 
resources have grown a lot in the last years, but we have not much for really 
unskilled persons.

I asked myself how I learned myself basics of programming and CS when I was 
young, but I can not remember well. We had a public library where I got many 
books, one was a nice Pascal book which I loved. Was not able to find it again. 
And I got some handwritten note from our teacher in school, and later also 
handwritten scripts in first years at university.

In the last years I also did some Google and amazon search for terms like 
"introduction to computer science and programming" but I was not satisfied. A 
lot of short tutorials, maybe 20 pages each. But that is too short. And on the 
other hand something like the Harward CS courses, which is too difficult and 
too detailed.


Re: Nim programming book for kids

2020-04-12 Thread Stefan_Salewski
> to teach programming to 12- and 13-year olds.

I know how it is sometimes done: You can move some smiley on the screen, you 
have a few ready made functions like goLeft(), goForward(), turnLeft(), 
touchWall() available. And then the kids can enter commands like "goForward; if 
touchWall() then turnRight()". Sometime that is done with real robots. May be 
some fun, but how is that related to CS and programming? It is more a logic 
game then. It is as when you learn in music school how to switch the radio on 
and how to adjust the equalizer.

Learning is not always fun. But I think it is important that there are some 
resources for learning available.

> spip

Of course the term kid is never used in the book. The subtitle is "A gentle 
Introduction". And in the introduction I wrote:

"But still there are people who missed this introduction in school for various 
reasons and decide later that they need some programming skills, maybe for a 
technical job. And there may exist some children that are not satisfied with 
the introduction to computer science taught at school. So we have decided to 
start this book with a short fundamental introduction to basic concepts -- most 
people can skip that part. In part II we explain the basics of computer 
programming step by step in a way which should enable even children to learn 
independently."

Of course the audience of the book is very small. It may grow when teachers in 
school would use the book as a foundation for their teaching.


Re: Platform dependent newline

2020-04-12 Thread Stefan_Salewski
It is still not exactly clear how "\p" works. Is it replaced at compiletime, 
that is that we would get the CR/LF when we compile on windows? Or is it 
processed at runtime, so whenever echo sees a "\p" on windows it outputs CR/LF?


Re: Nim programming book for kids

2020-04-12 Thread Stefan_Salewski
> The book would simply be boring to them.

I think computer science and programming is boring for most. As is physics and 
mathematics.

When someone does not love to learn something new, then all is boring, maybe 
with the exception of watching horror videos or playing action games?

Learning playing violin or the piano, learning chess, most what we learn at 
school can be regarded as boring.

For me the question is more if a bright 12 year old or a average 14 year old 
could understand it and learn from it. My goal would be that he can understand 
it, with the exception of the async and macros part maybe. I think he would be 
able with some help from teachers, friends, parents, but that help may not 
exist. But today we have the internet, there is wikipedia, there are forums and 
such.

I don't think that it makes sense teaching CS and programming on an even lover 
level.


Re: A pure Nim k-d tree

2020-04-11 Thread Stefan_Salewski
When its works in editors like VScode or neovim fine then all is ok, and I 
think it will as it is not that much generic.

My RTree is very generic, and nimsuggest has big problems with it. I was told 
that neovim is not really blocked, as it uses async LSP, but of course all is 
delayed much.

You may know that while RTree is designed for rectangles, it should work with 
point data fine. But your k-d-tree may work better for point data. 


Re: A pure Nim k-d tree

2020-04-11 Thread Stefan_Salewski
Nice, does it work with nimsuggest and Nim editors?

My RTree unfortunately blocks nimsuggest for 30 seconds permanently, which 
makes using it a bit hard. Maybe next winter I will compare your tree against 
my one.


Platform dependent newline

2020-04-11 Thread Stefan_Salewski
I think 4 or five years ago there was "n" as general newline char that could be 
two chars for windows and mac?

Now there seems to be "p" for that purpose? I missed the detailed discussion.

So would we now use "p" generally for terminal output. And "n" only when we 
want to guaranty that our program can not be used on windows?

On the other hand, there where rumors that newer windows versions works with 
"n"?

[https://nim-lang.org/docs/manual.html#lexical-analysis-character-literals](https://nim-lang.org/docs/manual.html#lexical-analysis-character-literals)


Re: OOP question

2020-04-10 Thread Stefan_Salewski
See the Nin manual, section

[https://nim-lang.org/docs/manual.html#multiminusmethods](https://nim-lang.org/docs/manual.html#multiminusmethods)

Note: Starting from Nim 0.20, to use multi-methods one must explicitly pass 
--multimethods:on when compiling.


Re: Compiling problem

2020-04-10 Thread Stefan_Salewski
Interesting that


var x, y, z = 3


Run

is allowed. Have not really guessed that a 3 vars would get initial value 3.

For your problem:

echo(x + y + z + a + b +c) is evaluated as echo(x + y + z + a + b(+c))

Because Nim can accept arguments without () as we use generally for echo.


Re: if error

2020-04-09 Thread Stefan_Salewski
When you use if as an expression like


var res = if cond: x else: y


Run

the x and y have to have the same type. Seems to be obvious in a statically 
type language, but I think I will add a note in my book.


Re: Nim programming book for kids

2020-04-08 Thread Stefan_Salewski
I would like to see a chinese version of the book, but please do no start 
translation before at least Part I and Part II are completed. I will tell you 
when.

But is translation not more work then writing from scratch? For me translating 
something from German to English takes generally very long, so I now write most 
text only in English, with the small set of word I have available in my head 
:-) But maybe translation engines can help you to get a first chinese draft.


Re: Nim programming book for kids

2020-04-08 Thread Stefan_Salewski
juancarlospaco

> PDF Download would be cool.

Why. I generated a PDF of my PhD thesis long time ago, it was written in LaTeX. 
But that times are over, we do not want to print documents these days, we do 
not want to kill trees. And PDF with its fixed format is bad for screens with 
different sizes. Note that the HTML is self -contained, at least the CSS is 
included, so that I would strongly assume you can download the HTML on your 
local computer to read it. I am not absolutely sure for the fonts, have to 
check that. Do not know really much about Asciidoctor yet. I may consider 
making an EPub when the book is complete -- well maybe a PDF too.

leorize

Thanks for that hint.

spip

> know if average children will understand

That is always the PROBLEM! I have recently heard about kids that had to do low 
level bit operations in C, without having a minimal understanding. As the kids 
could not understand, they had to ask in forum for solutions, copied that, of 
course without a basic understanding, teacher seems to be happy. And other kids 
learn how to play videos from inside python: Teacher: I have taught the kids 
video processing, I must be a great teacher, and my kids must be very 
intelligent, they manage Python so good that they can play even videos.

> what a compiler is Oh yes, i missed that, but would have found out myself for 
> first proof reading I guess.
> 
> References and Pointers a notion that must be introduced to child-programmers?

Maybe that is the reason why I started the book. Kids starting with Pascal had 
to learn, Kids using Nim have to learn it. Its hard to teach. I started gently, 
and explain it really well.

> and help you?

Yes, when the book is ready I would of course be grateful for smart 
proofreaders, with good Nim and english language skills. But it may take long 
to finish the book, indeed I have the impression that the two firsts parts was 
what really was missing, the other topics are more or less available. With the 
exception of the advanced stuff part, macros, async, threading, parallel 
processing. But that is more for Araq and mratsim to write.

cantanima

> headline is a bit misleading

I think the subtitle is fine: A gentle introduction. And the introduction of 
the book is fine also: "people who missed basic CS in school for some reason". 
"And in a way that even children should be able..."

Of course it is hard for children when there is no support by friends, parents, 
teacher. And one more problem with children is: When they do not start with 12 
being interested in CS, they generally never will.

> a book for beginners.

We have discussed that term really long enough. Is your beginner someone with 
absolutely no CS skills or someone having programmed in C++ and Haskell for 15 
years and is now comming to Nim? 


Nim programming book for kids

2020-04-07 Thread Stefan_Salewski
We had the discussion a few times in the last years -- if Nim is really a good 
start for kids and other people with absolutely no knowledge about CS.

Nim is fine for bright kids, sure.

But I hesitated generally to really recommend Nim for beginners, knowing that 
starting with the popular languages may be easier.

On the other hand -- generally Nim would be a fine beginner languages, with 
fine teaching materials and always an experienced Nim dev sitting beside you.

And when languages like C or Python are taught at school it seems to occur that 
that is sometimes done in a really bad and stupid fashion, and both of that 
languages are not perfect for teaching beginners.

The Nim situation has indeed improved in the last years, Nim community has 
grown a bit, and the number of tutorials has grown a lot.

Well, now there is one more. At least the first draft. Basically the result of 
maybe 10 days of work only. All was streamed directly from my head to the 
keyboard, so there may be some errors, due to the fine weather I have not done 
proofreading yet. But generally I am really satisfied with that start. Have no 
idea if I will work on it over the summer -- at least it is a start.

[http://ssalewski.de/nimprogramming.html](http://ssalewski.de/nimprogramming.html)


Re: Vapoursynth - optimization

2020-04-02 Thread Stefan_Salewski
> At this stage I can only recommend NOT to use VapourSynth.nim. It is too slow

Please note that your first task would be to make it work with default refc GC 
and with arc GC.

\--gc:none was only suggested for tests, as you reported that it crash with 
refc GC.|   
---|---  
  
For performance you would never use a seq[seq] for your filter matrix, but a 
continues block of memory which lives in the cache permanently. And you would 
like to use SIMD instructions. For SIMD you may try to code it manually, I 
think we have a simd module provided by mratsim, or you would write Nim code 
that can be converted by the Nim compiler in something for which the C compiler 
can apply SIMD instructions.


Re: Strange (maybe memory related behaviour)

2020-03-29 Thread Stefan_Salewski
I don't think that your problem is in some way related to the echo statement -- 
echo may in this case only trigger the bug, maybe because due to echo new 
memory is allocated or freed.

Such types of bugs are very hard.


Re: Strange (maybe memory related behaviour)

2020-03-29 Thread Stefan_Salewski
Ah yes, arc is available in devel. But try --gc:none, maybe also boehm.

Of course that will not really help, the bug is not Nim GC but your bindings, 
but maybe it gives somehow more insight.

I think valgrind does only help with --gc:arc and -d:useMalloc.


Re: Strange (maybe memory related behaviour)

2020-03-29 Thread Stefan_Salewski
> The function works fine, but after some time it fails

Are you using default refc GC or new --gc:arc?

I assume default refc. In that case I would assume that there is something 
wrong in your bindings, so when the GC starts for the first time it fails. You 
may call GC_Fullcollect() early, then the bug may occur earlier.

And you can try too compile with --gc:arc -d:useMalloc, then you have a more 
deterministic GC and the error may come earlier. In this case you can also try 
running your app by valgrind, valgrind may tell you details.

You can also play with --gc:none, maybe then your app runs longer, until all 
memory is consumed.

That are the first steps I would try...


Re: Submatrix function and arrays

2020-03-27 Thread Stefan_Salewski
It is hard to believe that it is OS related, but maybe it is. I did my test on 
Linux 64 bit.

Maybe you can ask on IRC hoping that someone has the same OS and Nim version as 
you.

First I had the impression that it may be related to other packages that you 
may have installed, but when you do not import them, then they should do no 
harm. 


Re: Submatrix function and arrays

2020-03-27 Thread Stefan_Salewski
> no idea how to fix this

I think you are only lazy :-)

The general approach would be to shrink and simplify the code -- the core 
problem seems to be the ==`() proc, so one would remove all what is not 
necessary to test this proc, and maybe to modify or simplify that proc. For 
example I would have started by replacing `func `==`*[W,H](m1, m2: Matrix[W, 
H]): bool = by proc isIdentical[W, H: static[int]](mx, my: Matrix[W, H]): bool =

Note that in your initial definition there is no space between W and H. Nim 
sometimes may love spaces.

The error message required type for m2: Matrix[==.W, ==.H] looks strange indeed.


Re: Submatrix function and arrays

2020-03-27 Thread Stefan_Salewski
The code from the playground link above compiles and runs fine with my Nim 
compiler, which is latest devil. Even with new --gc:arc option.


Re: Submatrix function and arrays

2020-03-26 Thread Stefan_Salewski
Fine that it works for you now.

For your equal test, I personally would never do it in that way, as with 
physics background we may work with really tiny values like plank constant (6 * 
10^-34) and many more really small values.

Maybe better somethink like


  (a/b - 1.0).abs < 1e-12


Run

I think I used something like that in my cdt module, but mratsim will know 
better...

The idea is to not test abs difference, but relative.


Re: Submatrix function and arrays

2020-03-26 Thread Stefan_Salewski
> So with the == is where I'm getting that error.

I have no idea what exactly your newMatrix(), your equal(), your check() and 
your test() is, so it is too hard for me to guess the error.


Re: Editor with nimsuggest support for libs with generics?

2020-03-26 Thread Stefan_Salewski
Is there any chance that this nimsuggest problem get fixes eventually, or is it 
not fixable?

It is the most serious nimsuggest problem, besides some templates which are 
displayed in editor as errors and some very rare crashes resulting from typing 
errors.

With generic modules like rtree a check takes 30 seconds and while editing the 
CPU load is really high unfortunately. 


Re: Submatrix function and arrays

2020-03-25 Thread Stefan_Salewski
Your error message makes no sense for me, and your proc compiles fine for me:


type
  Matrix*[W, H: static int] = array[W, array[H, float]]

func xsubmatrix*[W, H](m: Matrix[W, H]): Matrix[W - 1, H - 1] =
  for row in 0 ..< high(m):
for col in 0 ..< high(m[0]):
  result[row][col] = m[row][col]

# Remove specified row and column
func submatrix*[W, H](m: Matrix[W, H], rowIdx, colIdx: int): Matrix[W - 1, 
H - 1] =
  for row in 0 .. high(m):
var resultRow = row
if row == rowIdx: continue
elif row > rowIdx: resultRow.dec(1)
for col in 0 .. high(m[0]):
  var resultCol = col
  if col == colIdx:
continue
  elif col > colIdx: resultCol.dec(1)
  
  result[resultRow][resultCol] = m[row][col]

var
  m: Matrix[3, 3]

let s = submatrix(m, 1, 1)
echo s.len
echo s[0].len



Run


$ nim c -r t.nim
Hint: used config file '/home/stefan/Nim/config/nim.cfg' [Conf]
Hint: used config file '/home/stefan/Nim/config/config.nims' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: t [Processing]
Hint:  [Link]
Hint: 21948 LOC; 0.167 sec; 24.613MiB peakmem; Debug build; proj: 
/tmp/hhh/t.nim; out: /tmp/hhh/t [SuccessX]
Hint: /tmp/hhh/t  [Exec]
2
2
stefan@nuc /tmp/hhh $ nim -v
Nim Compiler Version 1.1.1 [Linux: amd64]
Compiled at 2020-03-13
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: 9eeb514dda08f1caadb0d8e01a8595d991530b52
active boot switches: -d:release
stefan@nuc /tmp/hhh $



Run


Re: (Meta) Why is there no beginners' section in the forum?

2020-03-25 Thread Stefan_Salewski
What is a Beginner? Someone who has a PhD in CS, has coded 20 years in C++ and 
Haskell, and now starts with Nim. Or a 12 years old Kid?

And is Beginners area where beginners answer questions of other beginners, 
maybe wrong?

For the NEWS I think we have the blog area of the website, but I agree a more 
active weekly news area may be good, maybe you can write that?

For Internals I think mosts devs are using IRC.

And general conclusion, sub-areas will make more sense when there is much more 
traffic than now. 


Re: Submatrix function and arrays

2020-03-25 Thread Stefan_Salewski
You may try


type
  Matrix*[W, H: static int] = array[W, array[H, float]]

func submatrix*[W, H](m: Matrix[W, H]): Matrix[W - 1, H - 1] =
  for row in 0 ..< high(m):
for col in 0 ..< high(m[0]):
  result[row][col] = m[row][col]

var
  m: Matrix[3, 3]

let s = submatrix(m)
echo s.len
echo s[0].len


Run


Re: {.this: self.} pragma

2020-03-24 Thread Stefan_Salewski
There is the with and the cascade package, maybe that is useful for you:


$ nimble search with
cascade:
  url: https://github.com/citycide/cascade (git)
  tags:macro, cascade, operator, dart, with
  description: Method & assignment cascades for Nim, inspired by Smalltalk 
& Dart.
  license: MIT
  website: https://github.com/citycide/cascade

with:
  url: https://github.com/zevv/with (git)
  tags:with, macro
  description: Simple 'with' macro for Nim
  license: MIT
  website: https://github.com/zevv/with



Run


Re: Gtk apps in Nim

2020-03-24 Thread Stefan_Salewski
> Why is that ? I am not looking into it as a cross platform framework.

Well, when it is fine for you that there is no Android support, then you can 
use GTK. Linux is fine generally, Windows is fine also, but some users will not 
be able to install GTK libs on their box, or just refuse to consider GTK at 
all. And MacOS is even more complicated.

And you have to learn GTK, if you want to create a serious app. Learning GTK is 
more effort than learning Nim. Of course if you want to create only some toy 
app, then you have not to learn much, but at least toy apps you should be able 
to create with the other Nim GUI toolkits too.

And finally, we do not know how complete Nim's GTK support really is. The Rust 
team has put a large effort in GTK support, and the other official GTK bindings 
Python, JavaScript, C++, D, Perl took much effort too.

In IRC logs you may find someone called Skylar talking about working on GTK/GDK 
from time to time, maybe he will provide something comparable to Rust at some 
time in future, I don't know.


Re: Unchecked arrays

2020-03-23 Thread Stefan_Salewski
You should be able to use a Nim seq for your task. Try it harder, and ask again 
when you really think that it is not possible.

UncheckedArray is generally used only for very low level stuff or when you get 
a C array from some library. Generally you should avoid using add() and cast, 
because only without Nim is really Memory safe.

I am not even sure if realloc() preserves the old content.

If You really like better to alloc your memory manually and use addr() often in 
ordinary code, then maybe languages like C or Zig are better for you. Well, I 
guess mratsim is using alloc(), cast and addr() too in all his libs, and maybe 
it is used in the compiler too. And it is used in languages bindings of course. 
But that are the low level parts of the Nim language then.


Re: How Seq work with objects ?

2020-03-22 Thread Stefan_Salewski
A Nim seq stores its entries in a consecutive memory area indeed. If you want 
to delete an arbitrary element, you have two possibilities: If you need to keep 
order, maybe as seq is sorted, you move all elements after the element do 
delete one position forward, which is a mem copy. Or, if order is not 
important, you can move last element in seq to place of element to delete. In 
all cases you have to set new length to len - 1 of course. We have del() and 
delete() for this. When you have to move, moving pointers is only 4 or 8 bytes 
each, and moving value objects is moving size of object each. So for really 
large elements moving pointers is faster of course. But generally a memcopy is 
very fast.

Pointers can be moved faster in seq maybe, but when you access the elements, 
then pointers are a indirection, and generally when you use pointers, then the 
elements itself may be distributed in Ram, so that there is nearly no cache 
support even when you process the data in consecutive order. 


Re: Gtk apps in Nim

2020-03-22 Thread Stefan_Salewski
There is

[https://forum.nim-lang.org/t/5759#35698](https://forum.nim-lang.org/t/5759#35698)

And my

NEd editor and my chess game.

And of course Aporia. Aporia is still GTK2, and NEd is still oldgtk3. The other 
two are gintro based. Additional some other people have used gintro for 
internal use I think, there was Mr lscrd and some older french sir from finance 
area. I myself am working on a vector drawing app, some sort of port from Ruby, 
but with low intensity.

But my general recommendation: We have nearly a dozen GUI tools for Nim now, 
try them first, and come back to GTK when all of them fail only.


Re: Gedit syntax highlighting.

2020-03-15 Thread Stefan_Salewski
No, the gtksourceview based editors Gedit, Aporia and NEd are really no 
recommendation for Nim. Aporia was GTK2 only and has not been updated in the 
last 4 years, Gedit has no Nim support at all, and NEd was written only as a 
plain test for oldgtk3. And gtksourceview itself -- well no one has really 
worked on its C lib code in the last years, internally it is really complicated 
and many features what I may want are missing, and I have not the deep 
understanding, time or motivation to work on it. (To be fair, there is still 
one gtksourceview dev, I can not recall his name, but I think he has made large 
contributions long time ago, then was burned out, and recently tried to come 
back. And there is the gnome boulder guy, but I think he does not care that 
much about gtksourceview itself.)


Re: Gedit syntax highlighting.

2020-03-14 Thread Stefan_Salewski
> I am skeptical

I am skeptical too -- why should they care when people not even care to ship an 
up to date file?

A never file is available here: 
[https://github.com/StefanSalewski/NEd](https://github.com/StefanSalewski/NEd)

But it is old too, its from 2016. Was working well with GTK3 gedit until 2 
years ago, but then somehow my gedit install seems to have deleted it locally, 
and I never tried to restore.


Re: Unknown performance pitfall in tables?

2020-03-12 Thread Stefan_Salewski
> let slice = repeated[i .. i + arg.high()]

My first guess is that this is not really optimal for performance, as it may 
allocate a new string for each loop iteration. You may make slice a string 
variable outside of the loop, and in the loop set first len to zero and then 
append the chars one by one by add(). But add() is still not optimal, so you 
may ensure first that slice is large enough, and then copy the char in using [] 
subscript operator.

Of course we may hope that the compiler would do all that for us.

To give compiler a better chance, we generally put all our code into a main 
proc, while you put the code following "when is main module" in global scope. 
And please try with --gc:arc, lets see how it compares.


Re: Nim 1.1.1/devel: programs silently crashing with --gc:arc

2020-03-06 Thread Stefan_Salewski
Have you tested with valgrind when your code has been compiled with \--gc:arc 
-d:useMalloc ?

Note that the default refc GC may hide bugs, as memory is freed with delay. 
While ARC does deterministic memory release at end of proc.

Maybe we should try address sanitizer of gcc or clang -- I have never tried 
that.


Re: Performance test agains Python

2020-03-06 Thread Stefan_Salewski
> Ok, I guess the string manipulations in Python are implemented with C as well,

Yes, most basic operations in Python are generally coded in C and are optimized 
well.

But what you can try: Put all your code in a main() proc. Whenever you do 
benchmarking, you should do that, in some cases it can increase performance 
drastically.


  1   2   3   4   5   6   7   8   9   >