with ?
[1/6] from: arolls::bigpond::net::au at: 4-Dec-2000 14:39
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.
[2/6] from: al:bri:xtra at: 4-Dec-2000 17:28
Anton wrote:
> 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?
Yes.
> 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 ?
Yes.
> 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])
>> o: make object! [
[ reverse_add: func [a b][print b - a]
[ Dialect: func [Dialect [block!]] [
[ do bind Dialect in self 'self
[ ]
[ ]
>> o/Dialect [reverse_add 5 9]
4
See:
http://members.nbci.com/AndrewMartin/Rebol/Units/HTML.r
for a practical example.
Andrew Martin
ICQ: 26227169 http://members.nbci.com/AndrewMartin/
[3/6] from: rebol:techscribe at: 4-Dec-2000 0:31
Hi Anton,
btw, if you'd rather not have value evaluated before it's assigned to
word in object, then use
set in object to word! :word :value
instead of
set in object to word! :word value
Take Care,
Elan
[4/6] from: rebol:techscribe at: 4-Dec-2000 0:21
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:
[5/6] from: g:santilli:tiscalinet:it at: 4-Dec-2000 13:04
Anton wrote:
> You know how other languages have a statement like this:
> with (obj){
<<quoted lines omitted: 4>>
> use a path notation like: obj/name obj/size etc.
> Does rebol have anything like this?
with: func [obj [object!] code [block!]] [
do bind/copy code in obj 'self
]
(untested)
HTH,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/
[6/6] from: arolls:bigpond:au at: 5-Dec-2000 10:36
On the money, Gabriele!
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted