Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor code quality improvements #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/LazilyInitializedFields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export @lazy, uninit,
init!, isinit, uninit!, islazyfield,
NonLazyFieldException, UninitializedFieldException, AlreadyInitializedException

using Base.Meta: isexpr

"""
Uninitialized
Expand Down Expand Up @@ -121,7 +122,8 @@ Function version of [@init!](@ref).
end

_check_setproperty_expr(expr, s) =
(expr isa Expr && expr.head === :(=) && expr.args[1].head === :.) || error("invalid usage of $s")
(isexpr(expr, :(=)) && isexpr(expr.args[1], :.)) || error("invalid usage of $s")

"""
@init! a.b = v

Expand Down Expand Up @@ -161,9 +163,8 @@ macro init!(expr)
return :(init!($(esc(body)), $(esc(sym)), $(esc(v))))
end


_check_getproperty_expr(expr, s) =
(expr isa Expr && expr.head === :.) || error("invalid usage of $s")
isexpr(expr, :.) || error("invalid usage of $s")

"""
isinit(a, s::Symbol)
Expand Down Expand Up @@ -247,7 +248,7 @@ macro uninit!(expr)
return :(uninit!($(esc.(expr.args)...)))
end

global in_lazy_struct
global in_lazy_struct::Bool
"""
@lazy struct Foo
a::Int
Expand All @@ -258,14 +259,14 @@ global in_lazy_struct
Make a struct `Foo` with the lazy fields `b` and `c`.
"""
macro lazy(expr)
if expr isa Expr && expr.head === :struct
if isexpr(expr, :struct)
try
global in_lazy_struct = true
return lazy_struct(expr, __module__)
return lazy_struct(expr)
finally
global in_lazy_struct = false
end
elseif expr isa Expr && expr.head === :(::) && length(expr.args) == 2
elseif isexpr(expr, :(::)) && length(expr.args) == 2
return lazy_field(expr)
else
error("invalid usage of @lazy macro")
Expand All @@ -279,11 +280,11 @@ function lazy_field(expr)
:($(esc(name))::Union{$Uninitialized, $(esc(T))})
end

function lazy_struct(expr, mod)
function lazy_struct(expr)
mutable, structdef, body = expr.args
structname = if structdef isa Symbol
structdef
elseif structdef isa Expr && structdef.head === :curly
elseif isexpr(structdef, :curly)
structdef.args[1]
else
error("internal error: unhandled expression $expr")
Expand All @@ -292,8 +293,8 @@ function lazy_struct(expr, mod)
expr.args[1] = true # make mutable
lazyfield = QuoteNode[]
for (i, arg) in enumerate(body.args)
if arg isa Expr && arg.head === :macrocall && arg.args[1] === Symbol("@lazy")
body.args[i] = macroexpand(mod, arg)
if isexpr(arg, :macrocall) && arg.args[1] === Symbol("@lazy")
body.args[i] = macroexpand(@__MODULE__, arg)
name = body.args[i].args[1]
@assert name isa Symbol
push!(lazyfield, QuoteNode(name))
Expand All @@ -308,8 +309,7 @@ function lazy_struct(expr, mod)
ret = Expr(:block)
push!(ret.args, quote
$(esc(expr))
# is @pure overkill?
Base.@pure $(LazilyInitializedFields).islazyfield(::Type{<:$(esc(structname))}, s::Symbol) = $checks
$(LazilyInitializedFields).islazyfield(::Type{<:$(esc(structname))}, s::Symbol) = $checks
function Base.getproperty(x::$(esc(structname)), s::Symbol)
if $(LazilyInitializedFields).islazyfield($(esc(structname)), s)
r = Base.getfield(x, s)
Expand Down