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 */
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
> 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
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.
> 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.
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
> 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
> 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
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
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
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`
> 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
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 :)
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)
> 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
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
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
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)
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
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
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
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
> 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.
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
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
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
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
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
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
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
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
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
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
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
if the book is really targeted at kids any translation would be helpful,
learning programming is hard enough.
> 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 **/*.*")
it works this way:
type
AuthenticatorType = enum
None,
Basic,
Session,
Cookie
Authenticator = object
case type: AuthenticatorType:
of None: discard
of Basic: username, passwor
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.
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
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
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.
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
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
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
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))
> 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
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
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
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
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
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
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
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
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)
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
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
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
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
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.
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
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
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
> 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
> 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.
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
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
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?
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
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
> 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
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
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
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
> 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
try putting the expression into parentheses:
var b = map(r, proc(x: string): string = (if x.isNilOrEmpty(): "stuff"
else: x))
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)
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?
> 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
78 matches
Mail list logo