Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@
(define (method-lambda-expr argl body rett)
(let ((argl (map arg-name argl))
(body (blockify body)))
;; If the method body mentions |#self#| but no parameter is called
;; |#self#|, introduce a local alias so var"#self#" works for
;; callable objects as it does for ordinary functions.
(let* ((have-self-arg? (memq '|#self#| argl))
(first (and (pair? argl) (car argl)))
(needs-alias?
(and (not have-self-arg?)
(expr-contains-p
(lambda (x) (eq? x '|#self#|))
body))))
(when needs-alias?
(set! body
(insert-after-meta
body
`((local |#self#|)
(= |#self#| ,first))))))
`(lambda ,argl ()
(scope-block
,(if (equal? rett '(core Any))
Expand Down
37 changes: 37 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4351,3 +4351,40 @@ let f = NoSpecClosure.K(1)
@test f(2) == 1
@test typeof(f).parameters == Core.svec()
end

# var"#self#"
# regular functions can use var"#self#" to refer to the function itself
regular_func() = var"#self#"
@test regular_func() === regular_func

# callable structs can also use var"#self#", which will refer to the struct instance
struct CallableStruct
value::Int
end
(obj::CallableStruct)() = var"#self#"
(obj::CallableStruct)(x) = var"#self#".value + x

let cs = CallableStruct(42)
@test cs() === cs
@test cs(10) === 52
end

struct RecursiveCallableStruct; end
(::RecursiveCallableStruct)(n) = n <= 1 ? n : var"#self#"(n-1) + var"#self#"(n-2)

@test RecursiveCallableStruct()(10) === 55

# In closures, var"#self#" should refer to the enclosing function,
# NOT the enclosing struct instance
struct CallableStruct2; end
function (obj::CallableStruct2)()
function inner_func()
var"#self#"
end
inner_func
end

let cs = CallableStruct2()
@test cs()() === cs()
@test cs()() !== cs
end