[racket] Read-only parameters

From: Gustavo Massaccesi (gustavo at oma.org.ar)
Date: Mon Feb 11 18:04:46 EST 2013

I was trying to define a "read-only" parameter, i.e. a parameter that
can be "read" and "parameterized", but not "written". I'd want to call
functions without being worried that the any of them changes that
parameter, but I want to allow each function to parameterize the
parameter for internal use.

Perhaps an "example" is clearer:
;-----
#lang racket/base
(require racket/port)

(define my-curr-out-port (make-parameter (current-output-port)))

(define (hello)
  (displayln "Hello" (my-curr-out-port)))

(define (quietly)
  (parameterize ([my-curr-out-port (open-output-nowhere)])
  (hello)))

(define (mute)
  (my-curr-out-port (open-output-nowhere))) ; I'd want an error here.

(hello) ;---> "Hello"
(quietly) ;---> ""
(hello) ;---> "Hello"
(mute)
(hello) ;---> "" :(
;---

Gustavo

Posted on the users mailing list.