Objects in REBOL

Discussion in 'Rebol' started by gordon, May 24, 2011.

  1. gordon

    gordon New Member

    I notice that in the documentation for make - if I make a prototype object which I then use as a template for subsequent objects I create, that each of the newly created objects is cloned from the template object.

    Does this mean then that if I define a function in the template object, that each of the new objects created from it will have its own independent copy of the function? From the description of objects in the documentation, it reads to me that this is indeed the case. If this is so, does this not mean that I am potentially wasting a ton of memory if I create hundreds of thousands or millions of these objects, and each one of them has its own copy of the same function?

    If I really want the function to behave identically in each object, I would like the field for the function to be a reference to the function rather than its actual value (the code block). Perhaps I am misunderstanding the docs and it is a reference rather than a value.

    Could anybody help me out?


  2. Graham

    Graham Developer Staff Member

    Sure ... but is using Rebol appropriate for applications with millions of objects?

    You could use a reference to a function if you don't want to have a copy in each object.
  3. MaxV

    MaxV Member

    Gordon, you could write here a sample of your object, so it's possible to understand better your needs.
  4. gordon

    gordon New Member

    Thanks Graham, Max, for your replies.

    I don't have a specific problem that I wish to solve, but I think it is worth it for me to keep in mind that when I use template objects, I should think about not putting stuff in them that I do not want to be needlessly duplicated because it is a waste of memory and a potential source of errors. In some circumstances, it is probably better to use a reference to a function as Graham suggests to avoid these issues.
  5. henrikmk

    henrikmk New Member

    Let's see:

    >> a: make object! [b: func []['boo]]
    >> a/b
    == boo
    >> c: make a []
    >> same? get in a 'b get in c 'b
    == false ; seems not the same
    >> append second get in a 'b 'foo
    == ['boo foo]
    >> get in a 'b
    >> probe get in a 'b
    func []['boo foo]
    >> probe get in c 'b
    func []['boo]


    Anyway, I don't use functions in objects that are meant for data, simply because they are hard to get rid of, if you need to transfer the object over network or have to store the content to disk. They are potentially also a security risk.

    BUT: Functions are highly useful in contexts, which are exactly the same as objects, but contexts are usually only used once, so there is no duplication. You are probably used to instancing, which REBOL doesn't really have. While objects can be used for for templates and extensively are, in many cases, REBOL creates them as separate contexts, so there is no internal difference between a prototype and a copy.

Share This Page