sevo
sevo

Reputation: 4609

How to manage stack in branched quotations?

If true quotation has zero arguments I can use when word because implicit false quotation also has zero arguments (does nothing).

But when I want to consume argument, I need else branch just to clean-up the stack. If logic were more complex, I imagine it might be tedious and error-prone re-factoring. Is there an easier way?

: print-if-dir ( directory-entry -- ) dup directory? [ name>> . ] [ drop ] if ;

Upvotes: 2

Views: 52

Answers (1)

Bill Sun
Bill Sun

Reputation: 165

You need to use a smart-when*:

USE: combinators.smart

: print-if-string ( object -- ) [ string? ] [ . ] smart-when* ;

Testing this out in the listener:

scratchpad: 2 print-if-string        ! Nothing happens
scratchpad: "2" print-if-string      ! Prints "2"
"2"

Upvotes: 1

Related Questions