> 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
You could put those even into an int. But yeah, sure. Though note that Variant
will not distinguish type aliases.
Can i put data types like HMENU, HWND, HPEN, HBRUSH, in a variant ?
> 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?
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.
@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
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
Could you please post a code snippet using "of" instead of "is"
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
Thanks man. Great help. :)
For inherited types (i.e. runtime types) you can use `of` instead of `is`.
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
> 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.
> I just want to check the type at runtime
[https://nim-lang.org/docs/typeinfo.html](https://nim-lang.org/docs/typeinfo.html)
> 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
Thanks a lot
Thanks for the tips. Actually, that was my question. I want to include type
name in that user defined type. Is it possible ?
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
Note that casting to a `ref` instead of a `ptr` is usually not safe.
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
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
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
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))
23 matches
Mail list logo