> On Nov 7, 2017, at 11:10 AM, Milo Turner <iital...@gmail.com> wrote: > > It is fairly easy to create a let-immutable form yourself
One thing to add to this. If you care about absolute safety, this isn't good enough because you can break it with a three-line my-set! macro: (define-simple-macro (my-set! x e) #:with x* (local-expand #'x 'expression '()) (set! x* e)) However, after talking with Milo about this I think it's still possible to prevent this. Expand to (#%expression internal-id) and use syntax-tainting to to prevent macros like `my-set!` from taking it apart. (define-for-syntax (immutable-variable-transformer internal-id) (make-variable-like-transformer (syntax-protect #`(#%expression #,internal-id)))) (define-syntax (let-immutable stx) (syntax-case stx () [(_ ([x e] ...) body ...) #'(let ([x e] ...) (let-syntax ([x (immutable-variable-transformer #'x)] ...) body ...))])) Then even if `my-set!` tries to take apart the #%expression to get to the internal-id: (define-simple-macro (my-set! x e) #:with (#%expression x*) (local-expand #'x 'expression '()) (set! x* e)) It won't succeed in breaking your let-immutable because the internal-id will be tainted. Alex Knauth > (define-for-syntax (immutable-variable-transformer new-id) > (make-set!-transformer > (λ (stx) > (syntax-case stx (set!) > [(set! x rhs) > (raise-syntax-error #f "cannot mutate immutable variable" stx)] > [(e ...) > (with-syntax ([ap (datum->syntax stx '#%app)]) > #'(ap e ...))] > [x new-id])))) > > (define-syntax (let-immutable stx) > (syntax-case stx () > [(_ ([x e] ...) body ...) > #'(let ([x e] ...) > (let-syntax ([x (immutable-variable-transformer #'x)] ...) > body ...))])) > > > On Sunday, November 5, 2017 at 5:15:24 AM UTC-5, Tony Garnock-Jones wrote: > Would it make sense to have a `let-immutable` form that was just like > `let` but that forbade use of `set!` with introduced variables? > > I'm thinking it could be handy for authors of libraries that introduce a > lot of bindings in DSLs where mutability has to be strictly controlled. > > I think it is probably possible to get the effect of `let-immutable` > with careful use of identifier macros, but would there be advantages > internal to the compiler/runtime of being able to up-front, primitively > declare a set of bindings as immutable? > > Tony -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.