Skip to content

Commit 13f32ed

Browse files
authored
Update limitations.md with REPL session (timholy#798)
1 parent d72d5c7 commit 13f32ed

File tree

1 file changed

+106
-21
lines changed

1 file changed

+106
-21
lines changed

docs/src/limitations.md

Lines changed: 106 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,119 @@ There are some kinds of changes that Revise (or often, Julia itself) cannot inco
88

99
These kinds of changes require that you restart your Julia session.
1010

11-
During early stages of development, it's quite common to want to change type definitions. You can work around Julia's/Revise's limitations by temporary renaming:
11+
During early stages of development, it's quite common to want to change type definitions. You can work around Julia's/Revise's limitations by temporary renaming. We'll illustrate this below, using `write` to be explicit about when updates to the file happen. But in ordinary usage, these are changes you'd likely make with your editor.
1212

1313
```julia
14-
# 1st version
15-
struct FooStruct1
16-
bar::Int
17-
end
18-
FooStruct = FooStruct1
19-
function processFoo(foo::FooStruct)
20-
@info foo.bar
21-
end
22-
```
23-
and then the type can be updated like
24-
```julia
25-
# 2nd version
26-
struct FooStruct2 # change version here
27-
bar::Int
28-
str::String
29-
end
30-
FooStruct = FooStruct2 # change version here
31-
function processFoo(foo::FooStruct) # no need to change this
32-
@info foo.bar
33-
end
14+
julia> using Pkg, Revise
15+
16+
julia> Pkg.generate("MyPkg")
17+
Generating project MyPkg:
18+
MyPkg/Project.toml
19+
MyPkg/src/MyPkg.jl
20+
Dict{String, Base.UUID} with 1 entry:
21+
"MyPkg" => UUID("69940cda-0c72-4a1a-ae0b-fd3109336fe8")
22+
23+
julia> cd("MyPkg")
24+
25+
julia> write("src/MyPkg.jl","""
26+
module MyPkg
27+
28+
export FooStruct, processFoo
29+
30+
abstract type AbstractFooStruct end
31+
struct FooStruct1 <: AbstractFooStruct
32+
bar::Int
33+
end
34+
FooStruct = FooStruct1
35+
function processFoo(foo::AbstractFooStruct)
36+
@info foo.bar
37+
end
38+
39+
end
40+
""")
41+
230
42+
43+
julia> Pkg.activate(".")
44+
Activating project at `~/blah/MyPkg`
45+
46+
julia> using MyPkg
47+
No Changes to `~/blah/MyPkg/Project.toml`
48+
No Changes to `~/blah/MyPkg/Manifest.toml`
49+
Precompiling MyPkg
50+
1 dependency successfully precompiled in 2 seconds
51+
52+
julia> processFoo(FooStruct(1))
53+
[ Info: 1
54+
55+
julia> write("src/MyPkg.jl","""
56+
module MyPkg
57+
58+
export FooStruct, processFoo
59+
60+
abstract type AbstractFooStruct end
61+
struct FooStruct2 <: AbstractFooStruct # change version nuumber
62+
bar::Float64 # change type of the field
63+
end
64+
FooStruct = FooStruct2 # update alias reference
65+
function processFoo(foo::AbstractFooStruct)
66+
@info foo.bar
67+
end
68+
69+
end
70+
""")
71+
234
72+
73+
julia> FooStruct # make sure FooStruct refers to FooStruct2
74+
MyPkg.FooStruct2
75+
76+
julia> processFoo(FooStruct(3.5))
77+
[ Info: 3.5
3478
```
79+
80+
Here, note that we made two changes: we updated the "version number" of FooStruct when we changed something about its fields, and we also re-assigned FooStruct to alias the new version. We did not change the definition of any methods that have been typed AbstractFooStruct.
81+
3582
This works as long as the new type name doesn't conflict with an existing name; within a session you need to change the name each time you change the definition.
3683
3784
Once your development has converged on a solution, it's best to switch to the "permanent" name: in the example above, `FooStruct` is a non-constant global variable, and if used internally in a function there will be consequent performance penalties. Switching to the permanent name will force you to restart your session.
3885
86+
```julia
87+
julia> isconst(MyPkg, :FooStruct)
88+
true
89+
90+
julia> write("src/MyPkg.jl","""
91+
module MyPkg
92+
93+
export FooStruct, processFoo
94+
95+
abstract type AbstractFooStruct end # this could be removed
96+
struct FooStruct <: AbstractFooStruct # change to just FooStruct
97+
bar::Float64
98+
end
99+
100+
function processFoo(foo::AbstractFooStruct) # consider changing to FooStruct
101+
@info foo.bar
102+
end
103+
104+
end
105+
""")
106+
107+
julia> run(Base.julia_cmd()) # start a new Julia session, alternatively exit() and restart julia
108+
109+
110+
julia> using Pkg, Revise # NEW Julia Session
111+
112+
julia> Pkg.activate(".")
113+
Activating project at `~/blah/MyPkg`
114+
115+
julia> using MyPkg
116+
Precompiling MyPkg
117+
1 dependency successfully precompiled in 2 seconds
118+
119+
julia> isconst(MyPkg, :FooStruct)
120+
true
121+
122+
```
123+
39124
In addition, some situations may require special handling:
40125
41126
### Macros and generated functions

0 commit comments

Comments
 (0)