An Example of using CCF with Smalltalk

(The main language with block-style but non-nestable comments is, of course, C, and that doesn't need CCF since it's got a more powerful preprocessor already. So I'll use Smalltalk instead.)

Here's a little routine that can either calculate for quite a while, or use a lookup table to get the answer faster. CCF decides which version to use. (Note: don't worry about understanding the (probably buggy) code. Just know that comments in Smalltalk start and end with the double-quote character, and watch what CCF does with them. And, at least in the version I used, ! is magic in the "chunk file", which is the external textual representation that CCF would work on.)


Version using an external Memo dictionary:

Integer method fib
  "recursive calculation of Fibonacci numbers"
  "## ccf:init"
  "we can't use bang easily, so ..."
  "## hide ?"

  | result |
  
  "## if ccf:defined (use_memo)"
  "I'll assume Memo is a Dictionary defined somewhere..."
  result := Memo at: self ifAbsent: [
  "## endif"
  self <= 1
    ifTrue: [ result := 1. ]
    ifFalse [ result := (self - 1) fib + (self - 2) fib. ].

  "## if ccf:defined (use_memo)"
  Memo at: self put: result.
  result ].
  "## endif"
  ^ result.

For comparison, a version that doesn't have the Memo:

Integer method fib
  "recursive calculation of Fibonacci numbers"
  "## ccf:init"
  "we can't use bang easily, so ..."
  "## hide ?"

  | result |
  
  "## if ccf:defined (use_memo)"
  "##? " "##? I'll assume Memo is a Dictionary defined somewhere..." "##?  "
  "##? result := Memo at: self ifAbsent: [ "
  "## endif"
  self <= 1
    ifTrue: [ result := 1. ]
    ifFalse [ result := (self - 1) fib + (self - 2) fib. ].

  "## if ccf:defined (use_memo)"
  "##? Memo at: self put: result. "
  "##? result ]. "
  "## endif"
  ^ result.