Skip to content

Add @typeconst #54117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ New library functions
* `logrange(start, stop; length)` makes a range of constant ratio, instead of constant step ([#39071])
* The new `isfull(c::Channel)` function can be used to check if `put!(c, some_value)` will block. ([#53159])
* `waitany(tasks; throw=false)` and `waitall(tasks; failfast=false, throw=false)` which wait multiple tasks at once ([#53341]).
* `@typeconst x = y` declares the global `x` as being of type `y`, aiming to replace the widespread use of `const` to improve the performance of globals ([#????]).

New library features
--------------------
Expand Down
34 changes: 34 additions & 0 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,40 @@ function def_name_defval_from_kwdef_fielddef(kwdef)
end
end


function typeconst_ex(ex)
ex isa Expr || return ex
if ex.head === :(=)
quote
tmp = $(esc(ex.args[2]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I wrote in https://giordano.github.io/blog/2022-06-18-first-macro/, you want to mark tmp as local to avoid leaking the name to the calling namespace

Suggested change
tmp = $(esc(ex.args[2]))
local tmp = $(esc(ex.args[2]))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I figured it was OK because it was gensymed (I had a previous version that called gensym() directly), but you're right it's even better not to leak it at all!

$(esc(ex.args[1]))::typeof(tmp) = tmp
end
elseif ex.head === :block
ex = copy(ex)
for i in eachindex(ex.args)
ex.args[i] = typeconst_ex(ex.args[i])
end
ex
else
ex
end
end
"""
@typeconst x = y

Declares the global `x` as being of constant type `y`, and performs the assignment `x = y`.
Equivalent to `tmp = y; x::typeof(tmp) = tmp`.
Typically used to improve the performance of globals.
Can also be used as `@typeconst begin x = 2; y = 3; end` to perform this replacement on several assignments.
"""
macro typeconst(ex)
if !(ex isa Expr && (ex.head in (:block, :(=))))
throw(ArgumentError("@typeconst: argument must be assignment or `begin` block"))
else
typeconst_ex(ex)
end
end

# testing

"""
Expand Down