Skip to content

Commit cb8d34e

Browse files
authored
Update globals to encourage type-annotation (#2)
1 parent abd8fe2 commit cb8d34e

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

test/globals.jl

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ using Test
88
# in the initial view (otherwise, you can hit 'w' to toggle "warn" mode).
99
# In this case, make sure you know the inferred types for `idx` and `charset`.
1010
# Once diagnosed, fix the code in src/globals.jl. To fix it, read
11-
# https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables
11+
# https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-untyped-global-variables
12+
# Here you can assume that the value of `charset` never needs to change.
1213
@test @inferred(getchar(5)) == 'e' # see `?@inferred`
1314
# Check `@descend` again at the end to see that you've fixed it!
1415
# (Chances are you'll have to restart Julia even if you're using Revise; there are some changes
@@ -18,11 +19,8 @@ using Test
1819
@test get_homedir() == "wrong"
1920
set_homedir(ENV["HOME"])
2021
@test get_homedir() == ENV["HOME"]
21-
# This one will fail. But you can't make the value constant and still update it!
22-
# Find a way to fix it. Hint: `?Ref` or use an array (Refs are like single-element arrays)
23-
# Explanation: the *container* can be constant, but the *contents* can be modified.
24-
# (Think of the container like a box: you can take something out of the box and put something different
25-
# inside it, but the box itself is unchanged.)
26-
# If the container type specifies the type of the contents, Julia's type-inference will be happy.
22+
# The next one will fail.
23+
# Be sure you follow the link on global-value type annotations in
24+
# https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-untyped-global-variables
2725
@test @inferred(get_homedir()) == ENV["HOME"] # again, this will likely require restarting Julia to test your fix
2826
end

0 commit comments

Comments
 (0)