Call by reference

What do you think of following demo code demostrating call-by-reference.

#lang typed/racket

( : addone : [Boxof Integer] -> Void )
(define addone
  (lambda (x)
    [begin
      (: ux : Integer )
      (define ux (unbox  x))
      (set-box! x (+  1  ux ))
      ]
    );lambda
  );define

(define abox (box 1))
(addone abox)
(define anumber (unbox abox))
(write anumber)

I'd be inclined to format it like this...

#lang typed/racket

(define (addone [x : (Boxof Integer)]) : Void
  (set-box! x (+  1  (unbox x) )))

(define abox (box 1))
(addone abox)
(define anumber (unbox abox))
(write anumber)

Not sure if that's what you're asking.

1 Like