That was my point... `foo` doesn’t have a `T: Numeric` version, therefore when it calls `isNumeric` the compiler has to call the version that always return false. `bar` is overloaded with both a `T` and a `T: Numeric` version, so there the compiler can call the most appropriate version of `isNumeric`.
- Dave Sweeris > On Oct 5, 2017, at 01:49, Alex Blewitt <[email protected]> wrote: > > I think one of your 'foo' or 'bar' is missing a <T:Numeric>, as otherwise the > functions are identical ... > > Alex > >> On 5 Oct 2017, at 09:23, David Sweeris via swift-users >> <[email protected]> wrote: >> >> >> On Oct 4, 2017, at 18:30, Kevin Lundberg via swift-users >> <[email protected]> wrote: >> >>> Can you do something like this? >>> >>> func isNumber<T: Numeric>(_ value: T) -> Bool { return true } >>> >>> func isNumber<T>(_ value: T) -> Bool { return false } >>> >>> I don't recall whether or not swift will pick the right version of the >>> function here, or whether this can even compile (mac isnt open at the >>> moment), but if you can overload functions in this way then it might be >>> much nicer than checking for lots of concrete types. >>> >> >> It’ll compile, but you’ll get the wrong answer if it’s called from another >> generic function that isn’t similarly overloaded for `T` vs `T: Numeric`. >> I’m not at my computer right now, but IIRC, this is correct: >> >> func foo<T> (_ value:T) -> Bool { >> return isNumber(value) >> } >> func bar<T> (_ value:T) -> Bool { >> return isNumber(value) >> } >> func bar<T: Numeric> (_ value:T) -> Bool { >> return isNumber(value) >> } >> isNumber(0) //true >> isNumber(“”) //false >> foo(0) //false >> foo(“”) //false >> bar(0) //true >> bar(“”) //false >> >> - Dave Sweeris >> _______________________________________________ >> swift-users mailing list >> [email protected] >> https://lists.swift.org/mailman/listinfo/swift-users >
_______________________________________________ swift-users mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-users
