Re: Referring to an immutable object's field?

2018-02-22 Thread StasB
Thanks!

Re: How to call original procedure from overloaded?

2018-02-22 Thread Stefan_Salewski
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

Can I call

2018-02-22 Thread r3d9u11
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:

Re: Referring to an immutable object's field?

2018-02-22 Thread twetzel59
Oh, and [here it is, for reference](https://nim-lang.org/docs/system.html#unsafeAddr,T).

Re: Possible compiler bug?

2018-02-22 Thread twetzel59
Looks like this has happened before. [Here is the issue](https://github.com/nim-lang/Nim/issues/6927).

Re: Referring to an immutable object's field?

2018-02-22 Thread twetzel59
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.

Re: Referring to an immutable object's field?

2018-02-22 Thread StasB
Thanks, guys. Is there any reason why unsafeAddr is never mentioned in the manual?

Re: Referring to an immutable object's field?

2018-02-22 Thread twetzel59
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

Re: Referring to an immutable object's field?

2018-02-22 Thread StasB
Is unsafeAddr always safe if the pointer doesn't outlive the scope of the argument?

Re: Referring to an immutable object's field?

2018-02-22 Thread twetzel59
Hmm, so you want a `ptr` to the data?

Re: Referring to an immutable object's field?

2018-02-22 Thread StasB
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?

Re: Referring to an immutable object's field?

2018-02-22 Thread twetzel59
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

Referring to an immutable object's field?

2018-02-22 Thread StasB
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],

Re: for loop and pairs question, explanation needed

2018-02-22 Thread r3d9u11
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 ;)).

Re: for loop and pairs question, explanation needed

2018-02-22 Thread boia01
The reason the first case is correct is because Nim implicitly calls `pairs` when the for loop has exactly 2 variables. See

Re: for loop and pairs question, explanation needed

2018-02-22 Thread Stefan_Salewski
> 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

for loop and pairs

2018-02-22 Thread r3d9u11
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

Re: Import module by name / absolute path

2018-02-22 Thread Udiknedormin
@guibar Thank you, guibar, I forgot I need a static[T] to change macros' semantics. I haven't used that for some time now.

Re: Basic pointer question

2018-02-22 Thread k1attila1
Ok thanks both of you to clarify this problem

Re: Basic pointer question

2018-02-22 Thread Stefan_Salewski
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

Re: Basic pointer question

2018-02-22 Thread mashingan
`p[0]` actually `(p[])[0]` because Nim is smart enough to know `p` is pointer so it lets you get away without deref it.

Re: Basic pointer question

2018-02-22 Thread k1attila1
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:= //

Re: Basic pointer question

2018-02-22 Thread Stefan_Salewski
> 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

Basic pointer question

2018-02-22 Thread k1attila1
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])

Re: Nim's status discussion

2018-02-22 Thread r3d9u11
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),

Re: Optimizing file I/O

2018-02-22 Thread Udiknedormin
@mratsim Thank you. I haven't read this blog post before, actually.