Hi Anton,

try

with: func [object [object!] block [block!] ] [
  foreach [word value] block [
    set in object to word! :word value
  ]
]


>> o: make object! [a: none b: none]
>> probe o

make object! [
  a: none
  b: none
]
>> with o [a: "this is a." b: "this is b."]
== "this is b."
>> probe o

make object! [
  a: "this is a."
  b: "this is b."
]

Note that this will generate an error if you pass a word in block that
does not exist in object.

A safer approach:

with: func [object [object!] block [block!] ] [
  foreach [word value] block [
    if in object to word! :word [
      set in object to word! :word value
    ]
  ]
]


This version silently skips all words in block that are not defined in
object.


Hope this helps,

Elan


Anton wrote:

> Hi all,
>
> You know how other languages have a statement like this:
>
> with (obj){
>         name = "object 1"
>         size = 23
> }
>
> which is to access elements of the obj object, without having to
> use a path notation like: obj/name obj/size etc.
> Does rebol have anything like this?
>
> Could a function be built into an object that accepts a block of
> code and does it inside the object, therefore no longer needing
> to explicitly refer to obj/name, just name, etc ?
>
> For example, if the special function is called 'with, the call
> would be something like:
>
> obj/with [name: "object 1" size: 23]
>
> The code then gets done inside the object.
> This should be equivalent to:
>
> obj/name: "object 1"
> obj/size: 23
>
> It's not urgent, just interesting.
> (My little attempt just now failed. -> with: func [code][do code])
>
> Anton.
> --
> To unsubscribe from this list, please send an email to
> [EMAIL PROTECTED] with "unsubscribe" in the
> subject, without the quotes.

-- 
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the 
subject, without the quotes.

Reply via email to