Re: How to convert integer to pointer

2018-11-23 Thread Stefan_Salewski
> I thought if is used in run-time while when is for compile-time, is not like > that? Generally it is of course. But I just tested what I wrote above, and my memory was indeed correct: "echo (a +1)" does not compile for your example code, but replacing if with when makes it compile again. As

Re: How to convert integer to pointer

2018-11-23 Thread yglukhov
You could put those even into an int. But yeah, sure. Though note that Variant will not distinguish type aliases.

Re: How to convert integer to pointer

2018-11-23 Thread kcvinu
Can i put data types like HMENU, HWND, HPEN, HBRUSH, in a variant ?

Re: How to convert integer to pointer

2018-11-23 Thread mashingan
> But then the if statement makes not much sense any more, it should be a when. > But when is compile time of course. So for me here is no actual runtime test > at all. I thought `if` is used in run-time while `when` is for compile-time, is not like that?

Re: How to convert integer to pointer

2018-11-23 Thread yglukhov
Here's something that could help: [https://github.com/yglukhov/variant](https://github.com/yglukhov/variant). And +1 in most cases you can/should go with static typing.

Re: How to convert integer to pointer

2018-11-23 Thread alehander42
@kcvinu This is a very interesting experiment, and I'll be interested in seeing if it's possible for it to work well. Still, keep in mind, that this is just veeery unusal in normal Nim code: I doubt I'll ever use anything like that even if it works, because 1) one can use normal types / variant

Re: How to convert integer to pointer

2018-11-23 Thread kcvinu
It is just an experiment. I love ArrayList in vb.net and list in python. I am trying to implement something like them in Nim. I know, that most of the time we can make a user defined type and declare a seq contains that type will work. But this is also a good feature of a language. This is my a

Re: How to convert integer to pointer

2018-11-23 Thread kcvinu
Could you please post a code snippet using "of" instead of "is"

Re: How to convert integer to pointer

2018-11-23 Thread alehander42
Hey @kcvinu, what are you trying to do generally with this approach? It seems interesting, but it's very rarely needed to do something like that in normal Nim code: I think you're trying to recreate some Python/Ruby code patterns, but maybe you can solve this in a generally different way (e.g. o

Re: How to convert integer to pointer

2018-11-23 Thread kcvinu
Thanks man. Great help. :)

Re: How to convert integer to pointer

2018-11-23 Thread mratsim
For inherited types (i.e. runtime types) you can use `of` instead of `is`.

Re: How to convert integer to pointer

2018-11-23 Thread Stefan_Salewski
mashingan, your above example looks a bit funny and strange to me. Your printout proc is generic, so due to actual calls with int, string and float parameters, we should actually get 3 distinct instances of that proc in executable. And actual parameter decides which actual instance is called. Bu

Re: How to convert integer to pointer

2018-11-23 Thread kcvinu
> Types exist at compile-time only (opposite to Ruby, Python and the like). I just want to check the type at runtime and want to make some changes in program. So i am assuming i cant do it in nim.

Re: How to convert integer to pointer

2018-11-23 Thread mashingan
> I just want to check the type at runtime [https://nim-lang.org/docs/typeinfo.html](https://nim-lang.org/docs/typeinfo.html)

Re: How to convert integer to pointer

2018-11-22 Thread LeuGim
> I want to include type name in that user defined type. Is it possible ? You can of coarse do anything with "type names" \- i.e. just strings "int", etc. But you probably want to store types themselves - `typedesc`. There's not so much you can do with them. Types exist at compile-time only (opp

Re: How to convert integer to pointer

2018-11-22 Thread kcvinu
Thanks a lot

Re: How to convert integer to pointer

2018-11-22 Thread kcvinu
Thanks for the tips. Actually, that was my question. I want to include type name in that user defined type. Is it possible ?

Re: How to convert integer to pointer

2018-11-21 Thread mratsim
Don't cast to refs, strings or sequences, those types are tracked by the garbage collector and the cast will not register those pointers with the garbage collector. Also I'm surprised you can have a typedesc field as in: type ListClass = ref object typ : typedes

Re: How to convert integer to pointer

2018-11-21 Thread Jehan
Note that casting to a `ref` instead of a `ptr` is usually not safe.

Re: How to convert integer to pointer

2018-11-21 Thread kcvinu
Thank you mratsim. I will use ByteAddress instead of int. Anyway, Can you please guide me on this ? type ListClass = ref object typ : typedesc pointR : ByteAddress var x = new ListClass x.typ = string x.pointR = cast[ByteAddress](addr

Re: How to convert integer to pointer

2018-11-21 Thread kcvinu
Ok, i 've got the partial answer. var s = cast[ref Sample](p)[] Run Now, one question arises. Is it possible to keep the type name and use it when needed ? I mean ; var abc = Sample var p = cast[int](addr(abc)) Run By this time, i know

Re: How to convert integer to pointer

2018-11-21 Thread mratsim
You can `cast[ptr T](yourInt)`, and to dereference: `yourPtr[]`. Instead of `int` I strongly suggest you to use `ByteAddress`. Here is an example of using pointer<->int conversion to align a pointer on 64-bit boundary. [Source](https://github.com/numforge/laser/blob/432e8f22ae63e798a9dcf49f2f2d

How to convert integer to pointer

2018-11-21 Thread kcvinu
Hi all, I have 2 questions. 1. How to convert an integer to pointer ? 2. How to dereference that pointer ? See this code sample type Sample = object of RootObj intVar : int strVar : string var abc = Sample var p = cast[int](addr(abc))