Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

No return Value???

 [1/7] from: tim:johnsons-web at: 28-Nov-2003 10:38


Hello Rebols: I've got a question: I've written a function called 'fetch, which has print stubs to help follow the flow control. If you evaluate 'print with 'fetch, no value is returned. Can someone tell me why this is so? TIA tim (example and code follows) Example:
>> do %test-fetch.r
Script: "Untitled" (none)
>> fetch 'abc
type = 'word val is unset returning unset words as default value == none
>> fetch 'print
type = 'word val is set The code for 'fetch and helper function 'safe-reduce follows: REBOL[] ; ------------------------------------------------------------------------------- make object! [ default-value: none set 'fetch func[ {safe data retrieval. Handles any value} val [any-type!] /seed {set default value} /deep {If block, reduce} /same {If unset return 'val} /local tmp][ either word? val[ print "type = 'word" either value? val[ print "val is set" get val ][ print "val is unset" either same[ print "returning unset word as the word" val ][ print "returning unset words as default value" default-value ] ] ][ ; any other type but 'word print ["type = " (type? val)] either block? val[ either deep[ safe-reduce/deep val ][ safe-reduce val ] ][ val ] ] ] ] ; ------------------------------------------------------------------------------- safe-reduce: function[blk[block!] {reduce block, leaving unset values} /deep {reduce nested blocks} ][rblk _reduce][ dummy: func[val /deep][val] ; pass ; below a function assignment using the prefix colon _reduce: either deep[:safe-reduce][:dummy] ; since 'reduce is a native value (compiled into binary) ; try it first either all[(not deep) (rblk: attempt[reduce blk])][ print "Used native 'reduce" rblk ][ print "Block with unset values, using 'safe-reduce" rblk: make block! length? blk foreach element blk[ either word? element[ either value? element[ append rblk (get element) ][ append rblk element ] ][ either block? element[ append/only rblk _reduce/deep element ][ append rblk element ] ] ] rblk ] ] -- Tim Johnson <[tim--johnsons-web--com]> http://www.alaska-internet-solutions.com

 [2/7] from: maximo:meteorstudios at: 28-Nov-2003 15:10


print does not return any value, I had to work around that in encompass... (hence the /silent refinement) you can build up a function which returns no value: a: func [data][ ; do what you want with data return unset 'data ] the above is not really usefull but if you want to free the memory used up by data (cause each word in a function keeps its value in between each function call), then this is one way of ensuring it does not hang around. ladislav and gabrielle might correct me if I'm wrong. -MAx --- You can either be part of the problem or part of the solution, but in the end, being part of the problem is much more fun.

 [3/7] from: SunandaDH:aol at: 28-Nov-2003 15:28


Tim,
> Can someone tell me why this is so?
*why* it doesn't return a value is a bit of a mystery. But, given that some functions don't, then any generic wrapper around a function needs to take that possibility into account. There was a discussion on the ML about this recently. It emerged from an entirely different topic; a search for "Please help me to promote REBOL" should find you the messages. I had to solve the same problem with the CGI debug code we wrap around all scripts at REBOL.org. There's a note on how that works at: http://www.rebol.org/cgi-bin/cgiwrap/rebol/documentation.r?script=cgi-debug.r# sect4.2 Sunanda.

 [4/7] from: tim:johnsons-web at: 28-Nov-2003 11:50


* Maxim Olivier-Adlhoch <[maximo--meteorstudios--com]> [031128 11:41]:
> print does not return any value,
Yeah, I know, I will probable have to use a tmp word, test it and use a default return value.
> I had to work around that in encompass... (hence the /silent refinement) > you can build up a function which returns no value:
<<quoted lines omitted: 131>>
> To unsubscribe from this list, just send an email to > [rebol-request--rebol--com] with unsubscribe as the subject.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.alaska-internet-solutions.com

 [5/7] from: rebol:techscribe at: 28-Nov-2003 14:25


Hi Tim. 1. You can use unset? to check for words that do not return values .
>> unset? print ""
== true
>> if unset? print "" [result: "<<unset>>"]
2. If you need to assign the return value of the tested function to some local word, you can use set/any to avoid an error. 3. Why doesn't print return a value? Wel;l, I suspect that is because the purpose of print is to display its argument. If print were to return a value, it would not only be displaying its argument, it would force the REBOL interpreter to display print's return value as well. I.e. you would see a combination of the printed argument and print's return value on the screen. Instead of
>> print "" >>
which is the expected result, for instance, you would get
>> print_with_return: func [arg] [ print arg return true ] >> print_with_return ""
== true
>>
BUT the argument to print was NOT "==true" it was "". Hope this helps. Elan Tim Johnson wrote:

 [6/7] from: nitsch-lists::netcologne::de at: 28-Nov-2003 23:47


Am Freitag, 28. November 2003 20:38 schrieb Tim Johnson:
> Hello Rebols: > I've got a question:
<<quoted lines omitted: 3>>
> returned. > Can someone tell me why this is so?
Eventually its for console-use? something like !>> print system/license reads better then !>> probe system/license ... == {REBOL End User License Agreement IMPORTANT. READ CAREFULLY. This License Agreement (AGREEMENT) is a legal contract between y... !>>
> TIA > tim >
-Volker

 [7/7] from: tim:johnsons-web at: 28-Nov-2003 15:35


* Elan <[rebol--techscribe--com]> [031128 13:42]:
> Hi Tim. > > 1. You can use unset? to check for words that do not return values . > >> unset? print ""
Thanks for reminding me.
> == true > >> if unset? print "" [result: "<<unset>>"]
<<quoted lines omitted: 131>>
> To unsubscribe from this list, just send an email to > [rebol-request--rebol--com] with unsubscribe as the subject.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.alaska-internet-solutions.com

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted