I'm using julia 0.4.1. Here's an example using ForwardDiff
foo(l, x) = ifelse( abs(x) < l, sin(x), cos(x) ) bar(l, x) = ifelse( isless(abs(x),l), sin(x), cos(x) ) derivative(x->foo(2,x), 1.) # ERROR: < not defined for ForwardDiff. GradientNumber{1,Float64,Tuple{Float64}} derivative(x->bar(2,x), 1.) # 0.5403023058681398 derivative(x->foo(2,x), 3.) # ERROR: < not defined for ForwardDiff. GradientNumber{1,Float64,Tuple{Float64}} derivative(x->bar(2,x), 3.) # -0.1411200080598672 immutable FooType v end value(f::FooType) = f.v Base.isless(f::FooType, x::Real) = value(f) < x Base.isless(x::Real, f::FooType) = x < value(f) FooType(3) < 4 # true FooType(3) < 1 # false 1 < FooType(3) # true 3 < FooType(3) # false