Skip to content

Instantly share code, notes, and snippets.

@singularitti
Created January 15, 2021 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singularitti/de4729e7a0d8444035d129de6e992e95 to your computer and use it in GitHub Desktop.
Save singularitti/de4729e7a0d8444035d129de6e992e95 to your computer and use it in GitHub Desktop.
Compare equality of 2 `struct`s #Julia
# See https://github.com/JuliaLang/julia/issues/4648#issuecomment-761030051
# ASSUMTION: `e1` and `e2` have the same run-time type
@generated structEqual(e1, e2) = begin
if fieldcount(e1) == 0
return :(true)
end
mkEq = fldName -> :(e1.$fldName == e2.$fldName)
# generate individual equality checks
eqExprs = map(mkEq, fieldnames(e1))
# construct &&-expression for chaining all checks
mkAnd = (expr, acc) -> Expr(:&&, expr, acc)
# no need in initial accumulator because eqExprs is not empty
foldr(mkAnd, eqExprs)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment