Skip to content

Commit

Permalink
- Minor changes to meta-files (Project.toml, make.jl, .gitignore)
Browse files Browse the repository at this point in the history
- Added simple memoization to _class_info.
  • Loading branch information
rjplevin committed Feb 6, 2019
1 parent 479e3df commit 7feb49a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.jl.*.cov
*.jl.mem
deps/deps.jl
docs/build
5 changes: 5 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"

[compat]
Documenter = "~0.20"
15 changes: 15 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Documenter, Classes

makedocs(
modules = [Classes],
sitename = "Classes.jl",
pages = [
"Home" => "index.md",
],

format = Documenter.HTML(prettyurls = get(ENV, "JULIA_NO_LOCAL_PRETTY_URLS", nothing) === nothing)
)

deploydocs(
repo = "github.com/rjplevin/Classes.jl.git",
)
4 changes: 2 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ using Classes

# Although Foo is immutable, subclasses might not be,
# so it's still useful to define this method.
function Foo(self::absclass(Foo))
function Foo(self::AbstractFoo)
self.foo = 0
end
end
Expand All @@ -210,7 +210,7 @@ end
bar::Int

# Mutable classes can use this pattern
function Bar(self::Union{Nothing, absclass(Bar)}=nothing)
function Bar(self::Union{Nothing, AbstractBar}=nothing)
self = (self === nothing ? new() : self)
superclass(Bar)(self)
Bar(self, 0)
Expand Down
12 changes: 11 additions & 1 deletion src/Classes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ end
# If a symbol is already a gensym, extract the symbol and re-gensym with it
regensym(s) = MacroTools.isgensym(s) ? gensym(Symbol(MacroTools.gensymname(s))) : gensym(s)

_cache = nothing

# Return info about a class in a named tuple
function _class_info(::Type{T}) where {T <: AbstractClass}
global _cache
_cache === nothing && (_cache = Dict())
haskey(_cache, T) && return _cache[T]

# @info "_class_info($T)"

typ = (typeof(T) === UnionAll ? Base.unwrap_unionall(T) : T)

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

return (wheres=wheres, ivars=ivars, super=superclass(typ))
result = (wheres=wheres, ivars=ivars, super=superclass(typ))
_cache[T] = result
return result
end

"""
Expand Down
13 changes: 13 additions & 0 deletions test/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
abstract type A end
struct B <: A
i::Int
end

expr1 = :(struct Foo1 i::B end)

class = B
expr2 = :(struct Foo2 i::$class end)

dump(expr1)

dump(expr2)
4 changes: 2 additions & 2 deletions test/test_classes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using Classes

# Although Foo is immutable, subclasses might not be,
# so it's still useful to define this method.
function Foo(self::absclass(Foo))
function Foo(self::AbstractFoo)
self.foo = 0
end
end
Expand All @@ -28,7 +28,7 @@ end
bar::Int

# Mutable classes can use this pattern
function Bar(self::Union{Nothing, absclass(Bar)}=nothing)
function Bar(self::Union{Nothing, AbstractBar}=nothing)
self = (self === nothing ? new() : self)
superclass(Bar)(self)
Bar(self, 0)
Expand Down

0 comments on commit 7feb49a

Please sign in to comment.