Skip to content

Commit 7feb49a

Browse files
committed
- Minor changes to meta-files (Project.toml, make.jl, .gitignore)
- Added simple memoization to _class_info.
1 parent 479e3df commit 7feb49a

File tree

7 files changed

+49
-5
lines changed

7 files changed

+49
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.jl.*.cov
33
*.jl.mem
44
deps/deps.jl
5+
docs/build

docs/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[deps]
2+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
4+
[compat]
5+
Documenter = "~0.20"

docs/make.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Documenter, Classes
2+
3+
makedocs(
4+
modules = [Classes],
5+
sitename = "Classes.jl",
6+
pages = [
7+
"Home" => "index.md",
8+
],
9+
10+
format = Documenter.HTML(prettyurls = get(ENV, "JULIA_NO_LOCAL_PRETTY_URLS", nothing) === nothing)
11+
)
12+
13+
deploydocs(
14+
repo = "github.com/rjplevin/Classes.jl.git",
15+
)

docs/src/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ using Classes
201201

202202
# Although Foo is immutable, subclasses might not be,
203203
# so it's still useful to define this method.
204-
function Foo(self::absclass(Foo))
204+
function Foo(self::AbstractFoo)
205205
self.foo = 0
206206
end
207207
end
@@ -210,7 +210,7 @@ end
210210
bar::Int
211211

212212
# Mutable classes can use this pattern
213-
function Bar(self::Union{Nothing, absclass(Bar)}=nothing)
213+
function Bar(self::Union{Nothing, AbstractBar}=nothing)
214214
self = (self === nothing ? new() : self)
215215
superclass(Bar)(self)
216216
Bar(self, 0)

src/Classes.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,16 @@ end
6767
# If a symbol is already a gensym, extract the symbol and re-gensym with it
6868
regensym(s) = MacroTools.isgensym(s) ? gensym(Symbol(MacroTools.gensymname(s))) : gensym(s)
6969

70+
_cache = nothing
71+
7072
# Return info about a class in a named tuple
7173
function _class_info(::Type{T}) where {T <: AbstractClass}
74+
global _cache
75+
_cache === nothing && (_cache = Dict())
76+
haskey(_cache, T) && return _cache[T]
77+
78+
# @info "_class_info($T)"
79+
7280
typ = (typeof(T) === UnionAll ? Base.unwrap_unionall(T) : T)
7381

7482
# note: must extract symbol from type to create required expression
@@ -79,7 +87,9 @@ function _class_info(::Type{T}) where {T <: AbstractClass}
7987
ivars = [_translate_ivar(d, iv) for iv in ivars] # translate types to use gensyms
8088
wheres = [_translate_where(d, w) for w in wheres]
8189

82-
return (wheres=wheres, ivars=ivars, super=superclass(typ))
90+
result = (wheres=wheres, ivars=ivars, super=superclass(typ))
91+
_cache[T] = result
92+
return result
8393
end
8494

8595
"""

test/example.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
abstract type A end
2+
struct B <: A
3+
i::Int
4+
end
5+
6+
expr1 = :(struct Foo1 i::B end)
7+
8+
class = B
9+
expr2 = :(struct Foo2 i::$class end)
10+
11+
dump(expr1)
12+
13+
dump(expr2)

test/test_classes.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using Classes
1313

1414
# Although Foo is immutable, subclasses might not be,
1515
# so it's still useful to define this method.
16-
function Foo(self::absclass(Foo))
16+
function Foo(self::AbstractFoo)
1717
self.foo = 0
1818
end
1919
end
@@ -28,7 +28,7 @@ end
2828
bar::Int
2929

3030
# Mutable classes can use this pattern
31-
function Bar(self::Union{Nothing, absclass(Bar)}=nothing)
31+
function Bar(self::Union{Nothing, AbstractBar}=nothing)
3232
self = (self === nothing ? new() : self)
3333
superclass(Bar)(self)
3434
Bar(self, 0)

0 commit comments

Comments
 (0)