diff --git a/src/LazilyInitializedFields.jl b/src/LazilyInitializedFields.jl index 659a0ba..6dcf6e0 100644 --- a/src/LazilyInitializedFields.jl +++ b/src/LazilyInitializedFields.jl @@ -56,6 +56,7 @@ export @lazy, uninit, init!, isinit, uninit!, islazyfield, NonLazyFieldException, UninitializedFieldException, AlreadyInitializedException +using Base.Meta: isexpr """ Uninitialized @@ -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 @@ -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) @@ -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 @@ -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") @@ -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") @@ -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)) @@ -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)