From 7feb49adf3ecd3cbdb18ef2df6fbef1b7e8ccdbb Mon Sep 17 00:00:00 2001 From: Richard Plevin Date: Wed, 6 Feb 2019 11:06:40 -0800 Subject: [PATCH] - Minor changes to meta-files (Project.toml, make.jl, .gitignore) - Added simple memoization to _class_info. --- .gitignore | 1 + docs/Project.toml | 5 +++++ docs/make.jl | 15 +++++++++++++++ docs/src/index.md | 4 ++-- src/Classes.jl | 12 +++++++++++- test/example.jl | 13 +++++++++++++ test/test_classes.jl | 4 ++-- 7 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 docs/Project.toml create mode 100644 docs/make.jl create mode 100644 test/example.jl diff --git a/.gitignore b/.gitignore index 381e0b6..8324c30 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.jl.*.cov *.jl.mem deps/deps.jl +docs/build diff --git a/docs/Project.toml b/docs/Project.toml new file mode 100644 index 0000000..c278538 --- /dev/null +++ b/docs/Project.toml @@ -0,0 +1,5 @@ +[deps] +Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" + +[compat] +Documenter = "~0.20" diff --git a/docs/make.jl b/docs/make.jl new file mode 100644 index 0000000..66d362d --- /dev/null +++ b/docs/make.jl @@ -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", +) diff --git a/docs/src/index.md b/docs/src/index.md index daa79ac..183707e 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -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 @@ -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) diff --git a/src/Classes.jl b/src/Classes.jl index a412cf7..b5d22a3 100644 --- a/src/Classes.jl +++ b/src/Classes.jl @@ -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 @@ -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 """ diff --git a/test/example.jl b/test/example.jl new file mode 100644 index 0000000..e110623 --- /dev/null +++ b/test/example.jl @@ -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) diff --git a/test/test_classes.jl b/test/test_classes.jl index 0e509e4..5034ebf 100644 --- a/test/test_classes.jl +++ b/test/test_classes.jl @@ -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 @@ -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)