Re: Need help for a generic object field

2018-11-25 Thread mratsim
If when assigning an object you don't want it to be copied, ref are a natural fit. For example a resource like database connection or a network socket, when you pass those around you don't want to suddenly create a new one, so use ref for those. Note that you usually don't create those in a tigh

Re: Need help for a generic object field

2018-11-24 Thread DTxplorer
Yes "ref" and "new" aren't the only way to create memory dynamically. I written my post too quickly.

Re: Need help for a generic object field

2018-11-24 Thread Stefan_Salewski
> If you want your program to spawn objects during runtime you'll need ref > objects. Not really. For example, when you have a single value object and add() it to a seq, it is copied -- similar as C++ vector does copy when you use std::vector::push_back(). You can modify your value object then,

Re: Need help for a generic object field

2018-11-24 Thread DTxplorer
If you want your program to spawn objects during runtime you'll need ref objects.

Re: Need help for a generic object field

2018-11-24 Thread kcvinu
The inheritance way seems to be more easy for me. The case statement in that type declaration looks like Chinese for me (just joking :)) Well, you showed 2 methods and stated the pros and cons of those 2 methods. Thats really helpful. Now one can easily choose what they want from this. Great. Le

Re: Need help for a generic object field

2018-11-24 Thread kcvinu
You seem to be attacking my each words carefully. But I do not want to take this as an attack. I would like to take this as a help as you said. I think there is no XY Problem in this case. Because you understood my real problem properly from my words. Well, You are right. My previous experience

Re: Need help for a generic object field

2018-11-24 Thread DTxplorer
I just read your answers carefully and I will tests these solutions, my project is much more complex than the example. Thank you all for taking the time to write such insightful answers.

Re: Need help for a generic object field

2018-11-24 Thread mratsim
Here you go: type Shape = ref object of RootObj Rect = ref object of Shape width:float height:float ConvexP = ref object of Shape vertices:seq[float] type Drawing = object # Don't use ref object if there is no need, th

Re: Need help for a generic object field

2018-11-24 Thread miran
> But strict typing is killing us I'll repeat what I have already tried to tell you several times, based on the examples/questions you have posted previously. I don't think 'strict typing' is what is killing you. It is your idea to do things "the old way", like you did them in some other langua

Re: Need help for a generic object field

2018-11-24 Thread miran
> I want an object field to be able to handle two differents types with a clean > syntax on the user side Take a look at [Object variants](https://nim-lang.org/docs/manual.html#types-object-variants) section in the manual, maybe that is something you can use in your case.

Re: Need help for a generic object field

2018-11-24 Thread Stefan_Salewski
> What is your suggestion ? When you already use ref object, you may consider inheritance. Or you may use sum types (union types) -- maybe bad if memory footprint of your objects differs a lot.

Re: Need help for a generic object field

2018-11-24 Thread GULPF
Sounds like you want [dynamic dispatch](https://nim-lang.org/docs/manual.html#multiminusmethods). Note that `Rect | Convex` is not a concrete type, so you can't have `seq[Rect | Convex]`.

Re: Need help for a generic object field

2018-11-24 Thread kcvinu
Are you sure ? A colon ? Not an equal sign(=) ?

Re: Need help for a generic object field

2018-11-24 Thread kcvinu
I also want to know the answer. I have tried a lot. But strict typing is killing us. Now, i can compile this code only if i use 2 distinct members in Drawing.

Re: Need help for a generic object field

2018-11-24 Thread juancarlospaco
` type RectOrConvex: Rect | Convex ` Run

Need help for a generic object field

2018-11-24 Thread DTxplorer
Sorry for that basic topic but I want an object field to be able to handle two differents types with a clean syntax on the user side. No problem if the code behinds the scene is a hacky template or something, but the user should never need to see that. I written a simplified example.