Deferred Words

Often, you know how to use a word X within word Y, but want to defer defining X for later or maybe want to use a variety of versions of X such as X1, X2.

  • The best way to do this is passing the XT into the word using the stack (i.e. function binding).
  • The alternative is to define a global word which has no real code body, then bind that name to an XT at runtime.
  • To create the "blank" word, we use 
    DEFER
    . For example, to create a blank word
    XX
    , we do the following:
    	defer xx
    	

    You can use the word 
    XX
    anywhere subsequently. To put in some functionality, use
    AS
    . The behaviour can also be changed subsequently at any time using
    AS
    .
    	
    	: x2 "pug!" . ;
    	
    	' x2 as xx
    	
    	xx
    	pug! ok	
    	

    Quiz

    Question 1

    Before putting in some functionality into 
    XX
    . What does
    DEFER XX
    do? Check your answer using
    SEE
    .



    Question 2

    What happens if we run 
    XX
    before it is given any functionality?



    Question 3

    	
    	: compose ( xt xt -- xt ) 
    	     { f g } [: f execute g execute ;] ;
    	

    Rewrite 
    COMPOSE
    without using local variables or local functions. Use
    DEFER
    instead.



    Next: Recursive Sequences