jackfirth / resyntax

A Racket refactoring engine
Apache License 2.0
56 stars 10 forks source link

let-to-define tricky case #148

Closed Metaxal closed 2 years ago

Metaxal commented 3 years ago

https://github.com/jackfirth/resyntax/blob/7fc36a29268f1210efa840c587881ad3fb4286e5/default-recommendations/let-binding-suggestions.rkt#L40

A recent example by Alexis King demonstrates that there may be more tricky cases where a let cannot be turned into a define: https://groups.google.com/g/racket-users/c/XkGnOMQKi2g/m/3AJJGaC4AgAJ?utm_medium=email&utm_source=footer&pli=1

Not sure how to detect or handle this one :-/

jackfirth commented 3 years ago

Things seem fine here actually. I took both the examples Alexis gave and wrapped them in functions (so that resyntax would see that they're in tail position) and manually applied the let-to-define migration myself. Here's what I ended up with:

(define (f)
  (let ([x 'outer])
    (define-syntax-rule (m a)
      (let ([a 'inner]) x))
    (m x)))

(define (f2)
  (define x 'outer)
  (define-syntax-rule (m a)
    (let ([a 'inner]) x))
  (m x))

(define (g)
  (let ([x 'outer])
    (define-syntax-rule (m a)
      (begin
        (define a 'inner)
        x))
    (m x)))

(define (g2)
  (define x 'outer)
  (define-syntax-rule (m a)
    (begin
      (define a 'inner)
      x))
  (m x))

Results:

jackfirth commented 2 years ago

Closing this as a duplicate of #10.