Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
test evaluating default values parameters in `@generated` functions
const U = Union{Type{Val{true}}, Type{Val{false}}}
f1(p::U = Val{false}) = p isa Type{Val{true}}
f2(p::U = Val{false}) = p isa Val{true} # false
f3(p::U = Val{false}) = p <: Type{Val{true}} # false
f4(p::U = Val{false}) = p <: Val{true}
f5(p::U = Val{false}) = p === Type{Val{true}} # false
f6(p::U = Val{false}) = p === Val{true}
f7(p::U = Val{false}) = p == Type{Val{true}} # false
f8(p::U = Val{false}) = p == Val{true}
@generated g1(p::U = Val{false}) = :($(p isa Type{Val{true}})) # false
@generated g2(p::U = Val{false}) = :($(p isa Val{true})) # false
@generated g3(p::U = Val{false}) = :($(p <: Type{Val{true}}))
@generated g4(p::U = Val{false}) = :($(p <: Val{true})) # false
@generated g5(p::U = Val{false}) = :($(p === Type{Val{true}}))
@generated g6(p::U = Val{false}) = :($(p === Val{true})) # false
@generated g7(p::U = Val{false}) = :($(p == Type{Val{true}}))
@generated g8(p::U = Val{false}) = :($(p == Val{true})) # false
for f in (f1,f2,f3,f4,f5,f6,f7,f8,g1,g2,g3,g4,g5,g6,g7,g8)
print("$f(Val{true}) : $(f(Val{true}) == true)\n")
print("$f(Val{false}) : $(f(Val{false}) == false)\n")
print("$f() : $(f() == false)\n\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.