FluxML/Functors.jl

empty namedtuple is not a leaf

CarloLucibello opened this issue · 6 comments

julia> Functors.isleaf((;))
false

I guess we should consider this a bug.

Discussion about this was in #36 (comment). Can you see any scenarios where having this be true would be helpful?

I didn't know about #36. I didn't think the question thoroughly yet but
right now on master everything is a leaf by default, which corresponds to children(x) == ().
Moving to the opposite default in #51 where everything is a tree,
one of the possible opt-out options one can choose is
@functor MyType () but since this gives children(x) == (;) MyType is not considered a leaf.

Maybe we should just have:

isleaf(x) = isempty(children(x))

?

Or I could introduce in #51 a @nofunctor MyT macro that does functor(x::MyT) = (), _ -> x. I think this should be done in any case.

The problem with using isempty is that children([]) == true. If anything, we would want to choose a type that has fewer methods defined and thus won't accidentally show up as a non-leaf. Nothing comes to mind, but if that's too generic then a custom NoChildren singleton type could work as well.

Assuming we want to stop specialcasing the empty tuple for isleaf, maybe better use a custom singleton rather than nothing so that we can make it (trivially) mappable.