Re: GDI-style lib for Nim.
So, in order to measure multiline text I need to count lines, measure interline spaces height somehow and multiple it all together ? ...Why it's so much easier in .NET ?
Re: cairoimpl.nim error
@cndkhuong have replied to your issue already at github: [https://github.com/StefanSalewski/gintro/issues/60](https://github.com/StefanSalewski/gintro/issues/60) If you wrote something here as a new forum user it may take some time until text will become visible.
Re: Nim beginners tutorial
> * Devote an introductory chapter to metaprogramming with a simple DSL > tutorial. > > * And a chapter to major databases connection. (especially SQLite). > > * I really wish you included a topic on concurrency and parallelism and how > you would normally use them in Nim. All these topics go beyond _beginners_ tutorial. I'll consider them for the "Intermediate Nim" tutorial if/when I begin to write it.
Walking trees without recursive iterators
In the [absence of support for recursive iterators](https://nim-lang.org/docs/manual.html#iterators-and-the-for-statement-first-class-iterators), how would you write an iterator returning all the nodes of a general tree? **GordonBGood** gave an [example](https://forum.nim-lang.org/t/5047#32049) of such an iterator for a binary tree and **mratsim** wrote a similar one for [binary trees stored in arrays](https://github.com/mratsim/weave/blob/v0.1.0/weave/datatypes/binary_worker_trees.nim#L91...3) in Weave, but here I want to walk over all the nodes of the following tree type: type Node = ref object value: string children: seq[Node] Run Whenever an iterator uses data structures with side effects or when not using tail call recursion, first flattening all the results in a queue/stack and then unqueing/unstacking them sequentially from the iterator is not possible. Also this flattening phase can be done only with bounded values iterators; you can't do it with an infinite data loop, or when the subsequent value of the iterator depends on the state of previous values... So what are the ways to write recursive iterators without using recursion?
Re: Nim beginners tutorial
Very nice but I really wish you included a topic on concurrency and parallelism and how you would normally use them in Nim. This is something the official tutorial pt 1 and 2 misses out on and would be a great addition to your tutorial.
assign an object variant kind for unsafe memory block
Hi, i tried create an object that has object variants using `alloc(size: Natural)` but when assigning the variant kind it throws an exception, is there a way for assign it without creating an auxiliar object or without using `-d:nimOldCaseObjects`?, here is a example: type Kinds = enum kA, kB, kC ExampleBlock = object case kind: Kinds of kA: a: int of kB: b: string of kC: c: uint16 otherFields: array[16, int] when isMainModule: echo "Sizeof block, ", sizeof(ExampleBlock) # Is a union after all let kindcrash = cast[ptr ExampleBlock]( alloc0(sizeof(ExampleBlock)) ) kindcrash.kind = kB # Crash Here kindcrash.b = "String Test" echo kindcrash.b Run
Re: GDI-style lib for Nim.
@Aiesha_Nazarothi: To get the height of a text line use canvas.getTextLineHeight().
Re: A pure nim GUI game for Linux & Windows
> it takes me 4 lines to choose 4 unique random colors from a sequence of 17; shuffle() should do the trick, not only in Nim: import sequtils, random, sugar var a = toSeq(1 .. 17).outplace(shuffle())[0 .. 3] echo a Run The other points you miss in NiGui -- well we have at least one lib which would support that :-) If you are really interested in someone improving or reading your game source code, then you may put the code on your github account. I think it is a great example for NiGUI.
Re: Nim beginners tutorial
Sorry for not getting back to you sooner. I was busy with some exams. I checked the new EPUB on my Kindle and it looked awesome. Extra white space problem is solved and the TOC works as expected. If its not too much to ask * Devote an introductory chapter to metaprogramming with a simple DSL tutorial. * And a chapter to major databases connection. (especially **SQLite** ).
A pure nim GUI game for Linux & Windows
As part of the process of learning Nim, I've ported my game _Gravitate_ (a variation of TileFall or the SameGame), to Nim. I used the [NiGui](https://github.com/trustable-code/NiGui) library, so no third party `.so`'s or `.dll`'s are needed. Although I've tried to make the most of Nim and to be as idiomatic as possible, I'm sure there's room for improvement. So I hope that some Nim experts will review the source and give me feedback -- or just edited `.nim` files -- to help me improve my code. (For example, in the `game.nim` file's `start()` function it takes me 4 lines to choose 4 unique random colors from a sequence of 17; surely it can be done in less code?) There are two archives available from the game's [home page](http://www.qtrac.eu/gravitate.html). The direct downloads (both including 64-bit executables and full source code <1 KLOC) are [Windows](http://www.qtrac.eu/gravitate-1.0.0.zip) and [Linux](http://www.qtrac.eu/gravitate-1.0.0.tar.gz). Things I couldn't do because either I can't figure out how or because NiGui doesn't support them: * Underline keyboard accelerator letters, e.g., the New game button shows "New", but I'd like the 'N' underlined. * Include icons in the buttons and to set the executable's icon all in code (e.g., by reading base64-encoded text from a file at compile time). * Show tool tips. * Do gradient fills. * Control focus, e.g., stop the buttons getting focus so you can "click" a tile with the Spacebar. (The current workaround is to press 'd' for delete tile.) * Enable/disable buttons, e.g., disable the Options dialog's OK button if any option is invalid. Despite the above, given how small and convenient NiGui is, and how it can be used to create stand-alone GUI executables, I think it is a superb library. And I'm also enjoying learning Nim very much!
Re: Nim is the friendliest language to start
@Stefan_Salewski his examples helped me to put the foot in the stirrup, for me I got tired of the basic docs, and I went on more concrete examples using an interactive project that led me to other horizon, type definition, create a module ... and SQL as all this to have a concrete interactive project and make the loop
Re: Editor with nimsuggest support for libs with generics?
Thanks for testing. So I think I will consider using your neovim. I have used GVim some years ago, so I know at least the most basic vim commands. My gentoo box has to install a lot of additional packages for neovin, that is the reason why I have not tested neovim for Nim already. Other reason was, that I was satisfied with my NEd editor. I think language server protocoll is not available for plain GtkSourceView, maybe it is available for Gnome-Builder-Editor. So maybe eventually I will modify NEd at some time in far future...
Re: binarySearch (from algorithm) not always working
https://nim-lang.org/docs/system.html#find%2CT%2CS
Re: binarySearch (from algorithm) not always working
Daaw! My bad - apologies for not knowing this...
Re: binarySearch (from algorithm) not always working
Well then there should be an assert inside binarySearch which should check that. It will be deleted in release build anyway
Re: binarySearch (from algorithm) not always working
What's the equivalent of python's index() on lists, for seqs/arrays in Nim?
Re: Translating C# code to Nim code. Help needed
You can ctrl+F it in the manual, but the difference between inheritable and RootObj is not that important. This is just a limitation of the parser, you must not use dot call syntax: var x: StorageBase discard Storage[int] x Run But if you just want cute syntax, you may as well use some metaprogramming not to deal with inheritance when it's not neccesary: template container(T: typedesc, size=100) = var `name` = newSeq[`T`] size template `@`(typ: typedesc[T]): untyped {.used.} = `name` container int container float @int[0] = 123 echo @int[0] # 123 echo @int[1] # 0 echo @int.len == @float.len # true Run
Re: Nim is the friendliest language to start
Ohhh - I think I missed that one... I looked at commander, docopts, and some others... ahh - this is the one from the crypto project. Awesome, I'll dig into it more! Thanks!
Re: Nim is the friendliest language to start
Confutils does CLI parsing generation and also conf files from JSON: [https://github.com/status-im/nim-confutils](https://github.com/status-im/nim-confutils)
Re: Editor with nimsuggest support for libs with generics?
Seems like nimsuggest doesn't like generics instantiations.
Re: Preview of Weave/Picasso v0.1.0, a message-passing based multithreading runtime.
> Does this aim to be part of Nim? No > Part of the standard library? It's probably too big, though some of the underlying code like the memory subsystem could be in the standard library. > Does it have an accessible api? spawn/sync/Flowvar are directly taken from [https://nim-lang.org/docs/threadpool.html](https://nim-lang.org/docs/threadpool.html). The parallelFor is just a for-loop.
Re: GDI-style lib for Nim.
Fixed my post, it was about **height**. Will report shadow soon.
Re: Editor with nimsuggest support for libs with generics?
Apparently your file managed to cause extreme slowdown for `nimsuggest`. While using editors with `nimlsp` or my `nim.nvim` plugin won't cause any lag due to asynchronous communications, the time it took for `nimsuggest` to return the results is extremely long. It took about 8-9 seconds on my machine for `nim check` to finish, and 22-30 seconds for `nim.nvim` to finish populating the highlighting in the file. Any queries to `nimsuggest` also took a decent amount of time before a reply could be seen. This renders `nimsuggest` useless against files like yours. But thanks to your file, I now have living proof that `nim.nvim` functions well even if `nimsuggest` stalls \o/.
Re: GDI-style lib for Nim.
NiGui provides getTextWidth (which regards line breaks) and getTextLineWidth (which is for one line of text). Can you please report the "text with black shadow" as GitHub issue? There shouln't be any shadow.
binarySearch (from algorithm) not always working
Trying to fix a bug and questioning my sanity, until I found this.. import algorithm echo binarySearch([8, 2, 5, 4], 5) # 2 echo binarySearch([8, 2, 5, 4, 7], 5) # 2 echo binarySearch([8, 2, 5, 4, 7, 0], 5) # -1 echo binarySearch([8, 2, 5, 4, 7, 0, 1], 5)# -1 echo binarySearch([8, 2, 5, 4, 7, 0, 1, 6], 5) # 2 echo binarySearch([8, 2, 5, 4, 7, 0, 1, 6, 3], 5) # 2 Run
Re: GDI-style lib for Nim.
So, some testing was done. First of all, disclaimer: I tried moving this micro-project from .NET due to excessive text measurement failures in _System.Drawing_ lib. ALSO, I wanted to add UI while finding myself to like wNim's layout engine much more than, say, WPF. Now, [https://github.com/emekoi/suffer](https://github.com/emekoi/suffer) . I quickly fixed their issues (wasn't that hard) just to discover that those lib just aren't able to request fonts from system - only load from binary files. Meh. Now, NiGui. Their drawing lib is 3/5 at best. Not only I found no getTextWidth (only getTextLineWidth) which was present even in VB6, it also renders text with black shadow by default. So, OK now. Any more suggestions ?
Re: Translating C# code to Nim code. Help needed
type Component {.inheritable.} = ref object A = ref object of Component x: int B = ref object of Component y: int var components: seq[Component] components.add A(x: 1) components.add B(y: 2) echo components[0] of A # true echo components[1] of B # true echo components[0].A.x# 1 echo components[1].B.y# 2 Run .
Re: Interfaces... why?
With interfaces, 1 procedure can handle multiple (n) types input for a parameter, as long as they implement the applicable interface required. With interfaces 1 procedure can return multiple types (n). With generics, it takes n (generated) procedures to handle n different types input as a parameter. With generics it takes n (generated) procedures to return n different types. I think interfaces are better the higher level you are in the program. MyInterface product = GetNextProductToProcess() ProcessProduct(product) Those two
Translating C# code to Nim code. Help needed
Hello, people of Nim : ) I'm translating my C# framework to the Nim lang. I understand that these languages don't share much in common so I would like to know how to achieve the c# functionality provided in the code below in the Nim way. > > namespace Pix > { > // just some classes of types that represent components > public class ComponentObject > { > } > > public class ComponentMotion > { > } > > public class ComponentHealth > { > } > > public class App > { > public App() > { >// I can get the Instance of particular storage by Type > Debug.Log(Storage.Instance.componentId); > Debug.Log(Storage.Instance.componentId); > Debug.Log(Storage.Instance.componentId); > } > } > > public class StorageBase > { > internal static int lastId; > } > > > public class Storage : StorageBase > { > public static readonly Storage Instance = new Storage(); > > public int componentId; > > public Storage() > { > componentId= lastId++; > } > } > } > > > > Run What I did in Nim type # I don't really need that StorageBase yet but experimented a bit with "OOP" StorageBase* = ref object of RootObj componentId*: int Storage*[T] = ref object of StorageBase components*: seq[T] var lastIdComponents = 0; # Storages proc newStorage*[T](size: int): Storage[T] = new(result) result.components = newSeqOfCap[T](size) result.componentId =lastIdComponents lastIdComponents+=1 type ComponentMotion = object x, y: float ComponentObject = object name: string var sComponentMotion = newStorage[ComponentMotion](100) var sComponentObject = newStorage[ComponentObject](100) Run But is there a way like in C# to "hide" storage vars so I could do something like Storage[ComponentMotion].components Run I'll use it in procs for getting components. proc componentMotion*(self: ent): ptr ComponentMotion = return addr sComponentMotion.components[self.id] Run Of course I can write manually everything but I'm curious how to go a little bit more abstract and want to learn more about more complex ways of working with Nim. Thanks in advance
Re: Nim is the friendliest language to start
I'm sorry - you seem to feel I was complaining? I really wasn't - just pointing out an area I thought would be good for nim bloggers. I don't really expect hand holding or what-not, and in fact have been really impressed with the help I've gotten from the Nim community. I've also gone thru some of the documents on the learning page, and continue to refer to them. I'm sure they're great if you're learning nim as a first language. What hasn't had time to develop yet, is the intermediate level docs. In my case, I do mostly utilty scripts around Terraform these days and system automation. So that's stuff like command line parsing, logging, config file parsing. All the boring stuff. So I'm mainly looking for Nim 'best practices', library recommendations, etc. My original post was simply to point out that if people wanted to blog/write articles about Nim, I personally (and of course, I could be wrong..) would think this sort of content would be appreciated and valuable helping people coming from other languages to learn the 'Nim way'. Thanks!