How to copy a Type

BlitzMax Forums/BlitzMax Programming/How to copy a Type

Hotcakes(Posted 2005) [#1]
Normally, I wouldn't have a problem with a memcopy between two types using VarPtr and SizeOf... but what happens if there's a reference inside the type...

That gets memcopied, but there would be no way for the garbage collector to know that has happenned... so I'm thinking there's probably some inbuilt way to copy a type, that for the life of me I can't find in the manual. Does anyone know if that's the case/what it is? =]


Azathoth(Posted 2005) [#2]
I think you need to make your own copy method.


ozak(Posted 2005) [#3]
This is standard in OOP systems. Implement a clone command or something :)


Hotcakes(Posted 2005) [#4]
That sucks. All these built in commands in the language and no CopyType... how hard could it be...


jwe(Posted 2005) [#5]
You need a deepCopy Command, so that an objects returns copies of its object.
Normaly a simple task to code if it would be possible to access methodnames and fieldnames in a Type.
All you would need then is a deepCopy method in your superclass that iterates over Methods and calls them dynamically.
But i think this is only possible with an Interpreter Language or is there a way to call a function when i only have a string representation ?

Like (psyeudocode)
func$="getName"
Object.call(func$)


tonyg(Posted 2005) [#6]
Copytype
type copy
type copy2


Hotcakes(Posted 2005) [#7]
Thanks tonyg, but all three of those methods use a straight memcopy, which suffers from new references not being picked up by the garbage collector.

I still don't understand why there isn't a built in method for this - surprisingly, FlameDuck's answer in one of those threads didn't help.


FlameDuck(Posted 2005) [#8]
how hard could it be...
Without runtime reflection? Impossible.

FlameDuck's answer in one of those threads didn't help.
Impossible. Here is a more thorough example:

I still don't understand why there isn't a built in method for this
Because BlitzMAX doesn't support runtime reflection.


Robert(Posted 2005) [#9]
Without runtime reflection? Impossible.


I don't think you are talking about reflection, see this article: http://en.wikipedia.org/wiki/Reflection_(computer_science)


ozak(Posted 2005) [#10]
You can't memcopy a class in any languages and it has nothing to do with reflection.
Even java which is heavily class based has a clone function that you have to override.


Koriolis(Posted 2005) [#11]
Without runtime reflection? Impossible.
Deeply wrong. Impossible for us BlitzMax users, certainly (without refelection). But not for Mark. A special command to copy an object is entirely doable. At least a shallow copy. For a deep copy, the problem is more that you'd need at least a way to tell the compiler which reference fields have to be taken into account in the deep copy (or else you'd very soon have an infinite recursion while deep-copying; not to say you are the only one to know which sub-objects would
simply make sense to deep copy).
Because BlitzMAX doesn't support runtime reflection.
So again, wrong. Especially interesting is the fact that what you quoted specifically mentions a "built-in" method, not something the BlitzMax users would write.


FlameDuck(Posted 2005) [#12]
I don't think you are talking about reflection
I am. If you can examine the structure of the object you want to copy at runtime - you can deep clone it.

You can't memcopy a class in any languages and it has nothing to do with reflection.
But if you had reflection, you could "rebuild" the class from scrap, and copy over it simple datatypes.

Even java which is heavily class based has a clone function that you have to override.
It has an interface (IClonable) that you have to implement. That doesn't mean that by using reflection you can't make your own "CloneAnything" functionality, as long as you're careful.

A special command to copy an object is entirely doable. At least a shallow copy.
Is there some significant difference, between a "built-in" shallow copy, and the memcopy methods mentioned above?


Michael Reitzenstein(Posted 2005) [#13]
The new references will be visible to the garbage collector.


Hotcakes(Posted 2005) [#14]
Yes. Thank you peoples, I'm not just dreaming here =]

I don't see any reason why the compiler can't crate a Clone method inside every type which could basically do a shallow copy exactly the same way we do it now - field by field. The compiler can figure out which fields are going to be used as references and as a result the garbage collector can know when those references have been duplicated.

Would save a shedload of typing.