Created
January 15, 2021 22:46
-
-
Save singularitti/de4729e7a0d8444035d129de6e992e95 to your computer and use it in GitHub Desktop.
Compare equality of 2 `struct`s #Julia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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