Thanks!
What you want seems to be to define a $ proc for type int and call system.`$`
from inside that proc.
proc `$` (x: int):string = "Integer " & (system.`$`(x))
echo 5
$ ./t
Integer 5
I was not sure that echo() would call our new proc, but
Hi! Can I call "original" procedure from overloaded? I Can't find any example
for this theme
for example:
proc `$` (x: int):string = "Integer " & ($x)
echo 5
is infinite recursion
as solution a tried to make additional procedure
proc toString (x:
Oh, and [here it is, for
reference](https://nim-lang.org/docs/system.html#unsafeAddr,T).
Looks like this has happened before. [Here is the
issue](https://github.com/nim-lang/Nim/issues/6927).
Wow, you're right. It should be!
I'm guessing the reason is that it's part of the stdlib, not the "language".
Really, though, it is a magical proc, and is implemented by the compiler, so
it's not an excuse. Maybe someone with a bit of experience (or write access to
the repo ) can add it.
Thanks, guys. Is there any reason why unsafeAddr is never mentioned in the
manual?
I am not sure about more complicated things, like alignment...
As for the general use case, your biggest risk is probably the GC. I see that
you use a stack `object` as opposed to a garbage-collected `ref object`, but it
is important to make sure that the C code immediately copies a value out
Is unsafeAddr always safe if the pointer doesn't outlive the scope of the
argument?
Hmm, so you want a `ptr` to the data?
Thank you! But what if, for example, I wanted to pass a pointer to the data to
a C function? Suppose I know for sure it won't mutate the data, and I want my
function signature to reflect this. Is there way to force Nim to give me an
address?
Sure thing!
You can declare a local `template` inside your `proc`.
type
Affine2* = object
data: array[2, array[3, float]]
proc product*(l: Affine2, r: Affine2): Affine2 =
template m: untyped = l.data
template n: untyped = r.data
Hello! I have some code like this:
type
Affine2* = object
data: array[2, array[3, float]]
proc product*(l: Affine2, r: Affine2): Affine2 =
Affine2(data: [
[
l.data[0][0] * r.data[0][0] + l.data[0][1] * r.data[1][0],
Thanks! (I was misled by string representation of pair. its string
representation is string representation of contained element. Also after
C/C++/C#/JS the syntax is still little bit unaccustomed for me, yet ;)).
The reason the first case is correct is because Nim implicitly calls `pairs`
when the for loop has exactly 2 variables.
See
> why first sample works?
Because it is correct.
pairs should give you a tuple, you can use it this way:
for index, item in ["a","b"]:
echo item, " at index ", index
for t in ["a","b"].pairs:
echo t[1], " at index ", t[0]
for index, t in
Hi. I have a little question: what differents between samples? And why first
sample works?
for index, item in ["a","b"]:
echo item, " at index ", index
# => a at index 0
# => b at index 1
for index, item in ["a","b"].pairs:
echo item, " at index ", index
@guibar Thank you, guibar, I forgot I need a static[T] to change macros'
semantics. I haven't used that for some time now.
Ok thanks both of you to clarify this problem
OK, I think I see your problem.
In Nim an empty subscript [] notation can be used to derefer a reference or
ptr. So if p is a pointer to string, in C++ you would write '*'p to dereference
it, but in Nim you write p[]. And in Nim you can write p[n] to access the n'th
character in that string. I
`p[0]` actually `(p[])[0]` because Nim is smart enough to know `p` is pointer
so it lets you get away without deref it.
Thank you
and i accept the second part of your answer, BUT the pointer isn't clear yet
if you try the given example you see :
a[0] ---> 'a'
p[0] ---> 'a' too. <\-- why not similar thing like ('*'p)[0] because p and a
are different things
in GOLANG :
> var s string = "alma"
> p:=
//
> why not p = a ?
Because Nim is statically typed. In your example a is a Nim string, and p a
pointer to a Nim string. So data types are different. When you apply addr() to
a string, you get a pointer to it. That is similar as it is in C++, where you
have C++ strings and can have pointers or
Hello
I have tried it :
var
a = "abc"
p: ptr string
echo(repr(a))
echo(repr(addr(a)))
p = addr(a) #The question : why not p = a ?
echo(repr(p))
echo(repr(addr(p)))
echo(a[0])
echo(p[0])
Wow, many thanks for detailed answer! This is very usefull post about Real
Valley! Under "MultiMedia engine" I mean library/framework/engine for making
visual crossplatform application, who can be rendered to opengl/directx/webgl,
something like a [Kha](https://forum.nim-lang.org/kha.tech),
@mratsim Thank you. I haven't read this blog post before, actually.
26 matches
Mail list logo