Search code examples
factor-lang

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 ;

Solution

  • 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"