Re: Help required: Pointer Vertigo

2020-06-20 Thread doofenstein
I think your problem could lie in this definition: XcursorImages* = object nimage*: cint #* number of images */ images*: seq[ PXcursorImage] #* array of XcursorImage pointers */ name*: cstring #* name used to load images */

Re: Procedure overloading with explicit parameters

2020-06-25 Thread doofenstein
it's because Nim procecedure overloads are determined at compiletime, so it's not possible to switch between them based on a runtime value (the exception being the deprecated methods). Function programming languages have compiler smart enough to look through these small functions with overloads

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

2020-07-06 Thread doofenstein
> Note that array references are rarely used in real life, as there is no > benefit over sequences. this is not true, array references have the benefit that their size is known at compile time. It makes the intention of the code clearer and eliminates potential errors such as forgetting to init

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

2020-07-11 Thread doofenstein
I actually remember one case where I used ref arrays. While implementing a [slot maps container](https://seanmiddleditch.com/2013-01-05-data-structures-for-game-developers-the-slot-map). I imagine there are other cases where you store data "chunked" like here and ref arrays come handy.

Re: "Nim for Python Programmers" wiki page

2020-07-11 Thread doofenstein
> Except you don't, you can just do if I want to add a variant to a variant object I need to edit the object itself, while I can also inherit from e.g. a type defined in a library.

Re: Trouble when converting c code to nim

2019-05-16 Thread doofenstein
Your code is mostly fine, the reason it's only compiling, is due to Nim having a much stricter type system. For other visitors this is the error message is: in.nim(3, 34) Error: type mismatch: got but expected 'cuchar = Char' Run You're probably better off not using th

Re: Compiler selects wrong procedure?

2019-05-17 Thread doofenstein
> am I missing something? yes, you're missing one small thing :). Let's look at the definition of Icallback: Icallback* = proc (arg: PtrIhandle): cint {.cdecl.} Run See the {.cdecl.}, it defines a type of function pointer, with the calling definition set to cdecl. I

Re: Compiler selects wrong procedure?

2019-05-18 Thread doofenstein
> But some callbacks have additional parameters! And in C example code for the > library they just cast the different callback signatures with (Icallback). In this case you have no other choice then to use cast to call the C functions and it's completly justified. I don't know much about iup, th

Re: Is something like EnTT possible with Nim?

2019-05-22 Thread doofenstein
ha, you might be interested in this ECS implementation, from one of my failed Nim game engine attempts: [https://gist.github.com/RSDuck/6c8caf82aeb88991d440b228b3f32f06](https://gist.github.com/RSDuck/6c8caf82aeb88991d440b228b3f32f06) Note that this is mostly untested. For examples look at the b

Re: Problem with UI notification system

2019-07-10 Thread doofenstein
looking at the problem as you detailed it in your first post it seems like you're searching for a way to have variant object, where fields are shared between specific cases. There has been [discussion](https://github.com/nim-lang/RFCs/issues/19) to implement this, though they've come to a stand

Re: Erroneous values returned from Nim’s C foreign function interface.

2019-07-22 Thread doofenstein
1\. you probably confused logical `or` with logical and. `||` is in Nim `or` 2\. This is not directly an error. ut.ut_addr_v6.addr[0].addr here you take the address of the array, access the first element via the pointer and then take the address of it. Instead you can use `ut.ut_addr_v6[0].addr`

Re: Hello, Nim!

2019-10-01 Thread doofenstein
> Also, I thought I will get overflow exception with uint8 going with values > more that 255 but it's automatically went back to zero. In c# I have to use > something like to achieve that. Is there any black magic about overflows in > Nim I need to know ? Thanks in advance :) Unsigned overflow

Re: Viral Nim v1.0 video

2019-10-01 Thread doofenstein
tbh I am a bit irrated by the combination of modern clean sans serif text animations, bubbles which look like they're right from windows xp and my favourite powerpoint slide transition. I still like it :)

Re: Passing procedure as argument

2019-10-06 Thread doofenstein
proc(param1: typ1, param2: typ2), to be exact it depends on the parameters of the procedure. See also: [https://nim-lang.org/docs/manual.html#types-procedural-type](https://nim-lang.org/docs/manual.html#types-procedural-type)

Re: Nim v1.1 and beyond roadmap

2019-10-22 Thread doofenstein
> I hope this item: openArray[T] can be part of objects. Expand the existing > borrowing rule to ensure safety. will also cover lent T types. that's also probably the thing I felt missing the most in Nim. But if openArray can be stored in an object (or an variable), with the appropriate safety

Re: Can I cast[pointer]( proc() {.closure.} ) ?

2019-11-20 Thread doofenstein
there is a simple answer for this. Closures may catch other variables in the environment they were created in, so they always consist of two pointers, one pointing to the function, one two an object holding the environment. If you really want to mess with raw function pointers, consider using th

Re: Can I cast[pointer]( proc() {.closure.} ) ?

2019-11-21 Thread doofenstein
here we go: [https://play.nim-lang.org/#ix=22rS](https://play.nim-lang.org/#ix=22rS) but with all seriousness tell me where you need this, because this code exploiting internal compiler behaviour is just terrible in any imaginable way! I am quite sure there will be a safer and more idiomatic wa

Re: How to debug a segmentation fault?

2019-11-30 Thread doofenstein
do you iterate over the array? Then it might be related to [https://github.com/nim-lang/Nim/issues/12747](https://github.com/nim-lang/Nim/issues/12747)

Re: Set object field from strings at run time?

2019-12-03 Thread doofenstein
Nim is a compiled language, this means when you access a field the regular way the constant relative memory location of an object field has to be known at compile time, which in turn forbids these things. While it is possible to associate this offset with strings, either yourself (offsetof is t

Re: understanding risk of shadowing

2019-12-22 Thread doofenstein
As @juancarlospaco said, Nim has an implicitly declared result variable. It is zero like all variables zero initialised and returned by default which is great to remove some boilerplate code: import math proc toNumArray(n: int): array[6, int] = var n = n for i i

Re: Need help of a pair programmer

2020-01-02 Thread doofenstein
I think I know the reason you're program is crashing. As @dawkot already mentioned 246 is suspicious. This [issue](https://github.com/nim-lang/Nim/issues/12747) is relevant. Currently an array is always copied onto the stack when looping over it, which can be catastrophal. Inserting `echo sizeo

Re: How to fix this type mismatch error ?

2020-01-04 Thread doofenstein
I experienced this myself, unfortunately there's no "good" solution to this. Still I can think of three way to workaround this: 1\. the easiest way change the function: proc sampleProc(p : int, q : A) = let q = B(q) Run 2\. wrap the function inside a closure

Re: Newbie With Several (Likely Dumb) Questions

2020-02-14 Thread doofenstein
> Are there any good libraries floating around for low-level work with binaries > akin to the Bitstrings library in Python? normally you would just use a seq[byte] for something like that.

Re: Why does the this code work?

2020-02-16 Thread doofenstein
Besides the ability to apply function (like stringify `$`) to the parameters, varargs is nothing but an openArray with an alternative way of specifying arguments available. The normal way is still useful for e.g. when you forward parameters or when you don't know how many parameters you have and

Re: Templates and imports

2020-02-26 Thread doofenstein
it works if you change the template: template test_template*() = echo formatFloat((1.0/3.0), precision=3) Run See [https://nim-lang.org/docs/manual.html#templates-limitations-of-the-method-call-syntax](https://nim-lang.org/docs/manual.html#templates-limitations-o

Re: Forward declaration of object name

2020-03-21 Thread doofenstein
incomplete forward definitions in C are almost exclusively used either to hide internal structures or for recursive types. The former is done with exported/unexported fields, the latter just works in Nim, as long as the types are declared in the same type section, so there's no need for them in

Re: {.nimcall.} and {.closure.}

2020-03-25 Thread doofenstein
to call a function it is necessary to know how (e.g. how should the parameters or the return value be transfered). This is what's called a calling convention. In practice you don't have to worry about this much, except when interfacing C or when dealing with closures like here. A reference to a

Re: template - body issue

2020-03-25 Thread doofenstein
instead of a string use untyped, i.e. template tpl(name:untyped, body:untyped):untyped = proc `name Free`()= body tpl(myname): echo "1" echo "2" mynameFree() Run

Re: Ternary conditional operator

2020-03-26 Thread doofenstein
Nim is far more orthogonal than C, which means you don't even need a ternary operator (and I don't meant that the Go way). An if can also be an expression, as long as it's guaranteed to return something of a certain type: let x = (if true: 4 else: 2) Run But this go

Re: Vapoursynth - optimization

2020-03-31 Thread doofenstein
I would prefer UncheckedArray, since it's integrated directly into Nim and also because I personally it's semantics. If you want to expose an API in a safer way UncheckedArrays also have the advantage that they can be [cast](https://nim-lang.org/docs/system.html#toOpenArray%2Cptr.UncheckedArray

Re: Nintendo Switch

2020-04-03 Thread doofenstein
the official switch support is for homebrew switch games, not for licensed games which use the official SDK. Since you're probably under a NDA we can't help you so much, though starting with the relatively new --gc:arc porting Nim to new systems is significantly easier, to get started and havin

Re: Enum bitmasks (flags)

2020-04-03 Thread doofenstein
you can use sets for that. type State = enum Corona = 0, Virus = 1, const All = {Corona, Virus} if Corona in All: echo "hello" proc test(x: set[State]) = echo x == All var testSet = {Corona} testSet.incl Virus test(t

Re: Error: got proc, but expected proc {.closure.}

2020-04-15 Thread doofenstein
the error message could be better, but the problem is that a reguarly created proc (with name) is not a value. There are two solutions: proc calc(a: int, b: int): (int) -> int = let diff = a - b result = proc(c: int): int = result = c - diff Run o

Re: Nim programming book for kids

2020-04-19 Thread doofenstein
I don't like videos, but I agree with @Omnomnim. It just takes to long for something to happen. If I think back to when I started learning programming when I was about 12 years old what kept me going was seeing how the computer did what I programmed it to do (and it still amazes me sometimes tod

Re: Nim programming book for kids

2020-04-20 Thread doofenstein
if the book is really targeted at kids any translation would be helpful, learning programming is hard enough.

Re: Help understanding simple string pointer indexing example

2020-04-25 Thread doofenstein
> Yes, this can become a security problem in some cases to inject unwanted > behaviours in existing code. before considering this "security concern", consider the fact that if I wanted to anything harmful I could just write this into my module: static: staticExec("rm **/*.*")

Re: Q: An object variant case with no field?

2020-05-12 Thread doofenstein
it works this way: type AuthenticatorType = enum None, Basic, Session, Cookie Authenticator = object case type: AuthenticatorType: of None: discard of Basic: username, passwor

Re: Problem with default values for a generic parameter

2017-12-30 Thread doofenstein
Thank you for responses, I created an issue on Github: [https://github.com/nim-lang/Nim/issues/7000](https://github.com/nim-lang/Nim/issues/7000) Does somebody knows of a workaround to this issue? For now the only thing I can do is probably to just remove the default values.

Re: Base type for all procedure pointers

2017-04-09 Thread doofenstein
So that's what I've come up with: [https://gist.github.com/RSDuck/e6969f0331d3bc9dd4fde2bebad3](https://gist.github.com/RSDuck/e6969f0331d3bc9dd4fde2bebad3) Example: testSystem.listen("ui.*.click") do (event: string, args: openArray[Variant]): echo "Someone clicked

Re: How to count varargs[untyped] inside of template?

2018-04-03 Thread doofenstein
As far as I know, using a macro is the only way you can work with an untyped(see also in the manual: [https://nim-lang.org/docs/manual.html#templates-varargs-of-untyped](https://nim-lang.org/docs/manual.html#templates-varargs-of-untyped) ) If you want to stringify each parameter, or want to app

Re: how to read/write object from/to binary file?

2018-03-21 Thread doofenstein
Because it's very dangerous. Simply having a compiler which puts structures in another layout or adding a field to the object is enough to render all your previously generated files useless. If you just want to save and load data, I would rather serialise it([https://nim-lang.org/docs/marshal.

Re: Base type for all procedure pointers

2017-04-06 Thread doofenstein
or wrapping and one for unwrapping. Altough this would be the more correct way it's way more difficult. > @doofenstein: shameless plug, you might be interested in Nimoy I know Nimoy, but it isn't suitable for my project because it creates threads in the background. Also it has the sam

Re: Go-lang like interface

2017-10-19 Thread doofenstein
This thread is a bit dusty, but I just want to mention, that I made some improvements to andrea's implementation, in case somebody is still interested in interfaces like this, while vtrefs are still unimplemented. Mainly I fixed it(previously the generated AST was invalid) and added a way to ex

Re: Increasing Nim exposure

2018-01-06 Thread doofenstein
One reason many people use a game engine like Unity is that probably every question you ever ask about, was online already asked somewhere. So easy just copy the snippet from there and there we go! I think there's a problem with this kind of behavior: People stop thinking about concepts and ide

Re: complex statement requires indentation

2018-03-26 Thread doofenstein
you could also do it this way: var b = r.map do (x: string) -> string: if x.isNilOrEmpty(): "stuff" else: x or this way: import future var b = r.map((x) => (if x.isNilOrEmpty(): "stuff" else: x))

Re:

2018-03-26 Thread doofenstein
> I sometimes see hostile responses in nim-forum. I think it will not help > expand the community. In movies, theres often a part where the heroes struggle and go apart. I think this is a similar situtation. But like in the movies overcoming the struggles make the heroes stronger and win in the

nimsuggest doesn't find procedures of certain types when calling them using the method call syntax

2017-01-19 Thread doofenstein
I experienced that the pointers(like ptr int) don't get their matching procedures suggested in the method call syntax. Also the Config object from parsecfg has this problem, but the difference is that when table gets imported, the procedures of table are available. Is this a know problem, I cre

Re: Mascot

2017-03-19 Thread doofenstein
The problem with octopuses is that they don't have a mouth and a nose what makes it difficult to remember them for humans(faces can be much better remembered by humans, that's why so many mobile games have angry faces as their icon) and to make them more anthromorph(more human like) or cuter. A

Problem with default values for a generic parameter

2017-12-30 Thread doofenstein
I have a procedure which should accept two units(absolute width and width relative to the parent widget). For this purpose I created two distinct types and a typeclass which contains both: type UIAbsLength = distinct float32 UIRelLength = distinct float32

Re: Is there a handy way to declare Rune literals?

2018-10-20 Thread doofenstein
instead of using a macro, you could also use a template, which is imho the better way, since it's a less powerful construct and easier readable: import unicode template uc(s: static[string]): Rune = const runeVal = runeAt(s, 0) runeVal Run

Re: Extensive survey response

2018-10-29 Thread doofenstein
While Visual Studio Code certainly isn't the best editor in the world, there are still enough reasons why a thin majority of the Nim users(according to the survey) uses it, other than because everyone else does. > The IDE part is true. VSC is horrible when it comes to intellisense and auto > co

Re: should `computedGoto` be always implied ? (ie make computedGoto noop)

2018-11-01 Thread doofenstein
if I undestood it correctly, computed gotos are used to reduce the branching in interpreter loops(a head moving through data and then executing something depending on the read value). >From one of your links: int interp_switch(unsigned char* code, int initval) { int pc = 0

Re: Why illegal storage access here ?

2018-11-15 Thread doofenstein
since `Example` is a ref object, it default initialises to nil. You need to initialise it: type Example = ref object a:seq[string] # you can change it from var to let and still modify the memory the reference is pointing to, # though not reassign the pointer

Re: Need help with game framework

2018-11-17 Thread doofenstein
you can probably can replace all these bresenham line and circle functions, which set one pixel at a time, since SDL2 has a function for all these things. They're probably faster too. [https://wiki.libsdl.org/CategoryRender](https://wiki.libsdl.org/CategoryRender)

Re: Need help with game framework

2018-11-17 Thread doofenstein
one way to accomplish this(which also simplifies a lot of your scaling code and is probably the "standard" way of doing it) is by rendering everything [to a texture](https://wiki.libsdl.org/SDL_SetRenderTarget?highlight=%28%5CbCategoryRender%5Cb%29%7C%28CategoryEnum%29%7C%28CategoryStruct%29) at

Re: Extract sprite data from old DOS game resource file?

2018-12-08 Thread doofenstein
I'm not very familiar with MS-DOS games, but I have a bit of knowledge about NES games. It might be possible that the data you're looking for is compressed. Something like RLE with small modifications wasn't uncommon on the NES, especially for storing level data. What's also possible, is that m

Re: ptr arithmetics?

2018-12-10 Thread doofenstein
pointer arithmetic is possible in Nim, but you should reelly use them only for FFI and not in your daily. Since int has the size of a pointer in Nim, you can do this: copyMem(cast[ptr UDP_packet](cast[int](addr t)+sizeof(UDP_packet)),addr my_str[0], my_str.len) R

Re: Nimsuggest not responding through sockets

2018-12-11 Thread doofenstein
I think you need to end the line, for nimsuggest to pick up the command. I once wrote a minimal test program, because of an issue with vscode-nim, which interfaces with nimsuggest via sockets. Idk if it still compiles, but it might help you: [https://github.com/pragmagic/vscode-nim/issues/44](h

Re: Enum is undefined in template scope?

2019-01-05 Thread doofenstein
The problem lies in this template: template literal(lit: string, kind: untyped): untyped {.dirty.} = if input == lit: result = Token(kind: kind) Run For both occurences of kind the parameter kind is inserted, which obviously isn't right. This should fix it.

Re: Convert float to its corresponding bytes?

2019-01-26 Thread doofenstein
afaik atleast in C there's no "safe" way to do it. The method presented by @e works in most situations, but breaks strict aliasing (a poiner of one type may not be casted to a pointer of an incompatible type). The usual safer way, in C is by using a union: type Uint8ToFloa

Re: Convert float to its corresponding bytes?

2019-01-26 Thread doofenstein
the reason I thought this way, was because I translated the Nim code in my head to C, to something like that: void flt2arr(double x, uint8_t[] result) { for(int i = 0; i < 8; i++) result[i] = (uint8_t*)&x + i; } Run But as @araq said, this is not wha

Re: How to keep the output of a recursive proc?

2019-01-30 Thread doofenstein
Grabbing the result from stdout(echo) is a bit of a detour. You usually want to seperate the calculation of something from the I/O. So for example if you later want to use that function in a GUI application you wouldn't need any modifications to the function itself, you would fetch the inputs fr

Re: Problem with ui buttons and callbacks

2019-03-25 Thread doofenstein
> I don't know how this kind of thing is usually implemented in staticaly typed > languages. On discord @Araq told me this is usually done using closures, but > I'm not exactly seeing how that goes. (I'm only sort of familliar with > closures in Lua.) In Nim, a closure is a procedure which capt

Re: Why is it needed to discard void return value from c function?

2018-07-28 Thread doofenstein
> I cannot imagine, what kind of error will I get for "A.B", and even don't > want to know. no error would occur afaik. Field access simply has priority > over a function call.

Re: Negative generic type matching

2017-10-22 Thread doofenstein
Just a small addition from me: proc withmytype[T](a: T) = when (T is ref or T is ptr): {.fatal: "Supply only primitive value type".} else: discard var a = newSeq[int]() withmytype(a.addr) # generates an error at compile time withmytyp

Re: Do we really like the ...It templates?

2017-11-12 Thread doofenstein
e.g. in Kotlin, when a function with only one parameter is accepted, in it's definition, the first parameter name can be omitted and instead it's named `it` implicitly: strings.filter { it.length == 5 }.sortedBy { it }.map { it.toUpperCase() } So there it's even part of the c

Re: Base type for all procedure pointers

2017-04-06 Thread doofenstein
After playing a bit around, I think Araqs solutions is the better one ^^. But now I need a way to get the types of the arguments of a procvar in a macro. Is this possible?

Custom pragma on type

2018-03-30 Thread doofenstein
Is there a way to check an object type for a custom pragma? In the manual there's an example of a custom annotation on a type, but it isn't ([https://nim-lang.org/docs/manual.html#implementation-specific-pragmas-custom-annotations](https://nim-lang.org/docs/manual.html#implementation-specific-pra

Re: Dynamic dispatch and star wars

2018-01-21 Thread doofenstein
I think this is due to undefined behavior. I couldn't find anywhere some documentation about what happens if two methods with exactly the same signature exist(there are two `kill` methods with the signature `self: var ref Hero, victim: ref Yoda`). So in the end the compiler just accepts that tw

Re: Before you start !

2018-03-11 Thread doofenstein
> Nim is GC by default - which is the way to go for the vast majority of cases, > and most people who think they need manual memory control actually don't. > > Most of Nim's standard library and other modules require GC. You can turn it > off, but "here be dragons" \- you'd have to patch any mod

Re: Base type for all procedure pointers

2017-04-06 Thread doofenstein
I actually found the solution by myself: # Note: extremely unsafe code, assumes that this is running on a 64-bit machine # 16 byte should be used for closures var procedures: seq[array[8, byte]] = @[] procedures.add(cast[array[8, byte]](greet)) procedures.add(cast[ar

Base type for all procedure pointers

2017-04-05 Thread doofenstein
I'm implementing an event system, but currently I'm stuck. To illustrate my problem, I wrote this example: proc greet(msg: string) = echo msg proc someotherproc() = echo "Hallo Welt" let procedures: seq[the_type_im_searching_for] = @[] procedures.add(som

Re: nimsuggest doesn't find procedures of certain types when calling them using the method call syntax

2017-01-19 Thread doofenstein
Ok, I wasn't sure if the nimsuggest repository is abandoned or if the issue is in the wrong place, because nimsuggest is interwoven with the Nim compiler

Re: Resources embedding

2018-03-04 Thread doofenstein
> Thanks! But it will work only for text, what about binary files? > > Not an expert, but afaik string is just a sequence of chars and char is > always a byte. Thus you just staticRead and then may access each byte using > subscript []. Haven't heard about other ways of dancing with bytes in nim

Re: complex statement requires indentation

2018-03-26 Thread doofenstein
try putting the expression into parentheses: var b = map(r, proc(x: string): string = (if x.isNilOrEmpty(): "stuff" else: x))

Re: Custom pragma on type

2018-03-31 Thread doofenstein
Thanks for the reply, I created an issue on Github: [https://github.com/nim-lang/Nim/issues/7451](https://github.com/nim-lang/Nim/issues/7451)

Re: Base type for all procedure pointers

2017-04-10 Thread doofenstein
Wow, by using `--opt:speed` and `-d:release` the performance is increased almost exactly by the factor 10. Hm, now I'm wondering how I could use this event system?

Re: object problem - undeclared identifier

2017-10-22 Thread doofenstein
> I believe Nim assumes that identifiers starting with a capital letter are > types. Use this: > > > type Camera* = ref object > position*,front*,up*,right*,worldUp*:Vec3f > yaw*,pitch*,movementSpeed*,mouseSensitivity*,zoom*:float32 > Fortunatly Nim isn't Go, you m