Sew
1 Sew Directives
8<-plan-from-here
8.6

Sew

 #lang sew package: sew-lib

Sew is a Racket language that makes it easy to add boilerplate that surrounds the rest of the file, without changing its indentation.

#lang sew racket/base
 
(require (only-in (racketmodname sew) 8<-plan-from-here))
 
[8<-plan-from-here <>
  #'(begin
      (provide main)
 
      (define (main)
        <> ...))]
 
(displayln "Hello, world!")

The expression directly after the [8<-plan-from-here <>] line is evaluated in phase 1, with <> bound as a template variable to the rest of the content of the file.

This can come in handy for maintaining the file as though it belongs to a certain tradition of writing modules, while it actually belongs to another. For instance, the module above can be written as though it’s a script, but it’s actually a module that provides a main function.

In the Lathe project, we intend to use Sew to write a module once but compile it with multiple levels of contract enforcement.

For now, 8<-plan-from-here is the only directive defined by Sew. We might extend this in the future to allow files to be cut up into more than one piece before they’re all sewn together. This may resemble literate programming techniques for assembling programs in terms of chunks.

    1 Sew Directives

1 Sew Directives

syntax

[8<-plan-from-here rest-of-file-id preprocess-expr]

This must be used at the top level of a module using #lang sew. It binds the rest of the forms in the module to the pattern variable #'(rest-of-file-id ...) as it runs preprocess-expr in the syntax phase. The result of that expression is then used as the replacement (expansion) of this form and all the rest of the forms in the file.

For instance, the usage site

[8<-plan-from-here <>
  #'(begin
      (provide main)
 
      (define (main)
        <> ...))]
 
(displayln "Hello, world!")

expands into this:

(begin
  (provide main)
 
  (define (main)
    (displayln "Hello, world!")))