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

add @leaf macro #55

Merged
merged 5 commits into from
Dec 4, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
move code around
  • Loading branch information
CarloLucibello committed Nov 29, 2022
commit 7558016659e6807cce92a98e3bf9d08d912e7d3e
28 changes: 14 additions & 14 deletions src/functor.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
function functor end

@leaf Any
const NoChildren = Tuple{}

function makeleaf(m::Module, T)
@eval m begin
CarloLucibello marked this conversation as resolved.
Show resolved Hide resolved
$Functors.functor(::Type{<:$T}, x) = $Functors.NoChildren(), _ -> x
end
end

macro leaf(T)
:(makeleaf(@__MODULE__, $(esc(T))))
end

@leaf Any # every type is a leaf by default
functor(x) = functor(typeof(x), x)

functor(::Type{<:Tuple}, x) = x, identity
Expand Down Expand Up @@ -31,19 +44,6 @@ macro functor(args...)
functorm(args...)
end


const NoChildren = Tuple{}

function makeleaf(m::Module, T)
@eval m begin
$Functors.functor(::Type{<:$T}, x) = $Functors.NoChildren(), _ -> x
end
end

macro leaf(T)
:(makeleaf(@__MODULE__, $(esc(T))))
end

isleaf(@nospecialize(x)) = children(x) === NoChildren()

children(x) = functor(x)[1]
Expand Down