How to check a value is defined in an object?

Discussion in 'Rebol' started by endo, Feb 27, 2010.

  1. endo

    endo New Member

    Hi, I would like show how to check a value is defined in an object or not. I decided to post this to show the usages of found?, value?, in and get functions.

    Here is the situation, O is an object, there a two words defined in O.

    O: context [A: 3 B: none]

    B is defined and its value is none. C is not defined in O. And that is what I want to show.
    You can try using get and found? functions:

    found? get in o 'A
    >> true

    found? get in o 'B
    >> false

    found? get in o 'C
    >> false

    This is because found? simply tests the value is none or not. (Note that false and none are different in Rebol. "found? false" will return true)

    So we could not recognize the word B is defined or its value is none. Same question is for C.

    You can get value of a word inside O using get:

    get in o 'A
    >> 3

    get in o 'B
    >> none

    get in o 'C
    >> none

    This is why we could not use found? and get together. So you may thing that we can use value? function. "value? returns TRUE if the word has been set." the document says.

    value? get in o 'A
    >> true

    value? get in o 'B
    >> true

    value? get in o 'C
    >> true

    Hmm.. this is because "get in o 'C" returns none. And none IS a value in Rebol.

    get in o 'C
    >> none

    value? none
    >> true

    So lets look closer,

    in o 'A
    >> A

    in o 'B
    >> B

    in o 'C
    >> none

    in returns the word itself if it is defind in the object, not its value.
    This is why we cannot use value? and in together. So our mistake is to use get function. The solution is simple:

    found? in o 'A
    >> true

    found? in o 'B
    >> true

    found? in o 'C
    >> false

    This is what we want. So we can write this small function:
    defined?: func [w [word!] o [object!]] [found? in o w]

    defined? 'A O
    >> true

    defined? 'B O
    >> true

    defined? 'C O
    >> false

    Well, this is how I learnt the meanings of in, get, found? and value? functions.
    "get a value of a word in an object, if it is none than it is not defined." INCORRECT! And I made this mistake several times when learning Rebol.
    I hope this will be helpful for beginners.
  2. YueM

    YueM New Member

    thanks Endo for taking the time to share your experiments, and your findings. I am sure it will help other beginners.
  3. endo

    endo New Member

    Thanks Yuem,
    The title should be "How to check a WORD is defined in an object?" not VALUE I guess.
  4. Gerard

    Gerard New Member


    I just wanted to share with you some interesting view I found on the Net about REBOL and its particular way of binding words with both values and their respective contexts.

    It's been posted by Gregory Higley and he shows a deep understanding of the way REBOL works ...

    Here it is on the Loudly Recursive blog :
  5. endo

    endo New Member

    Yes I already read this blog post, and it is a very deep and nice explanation of binding in rebol. Thanks for sharing it here.

Share This Page