It seems to be already working that way in kmath. The type is inferred in compile time, but the expression itself is universal for all types. Also there is an example for expression that is defined outside of context, but it is tricky to make it perform fast.