Hacker News new | comments | show | ask | jobs | submit login

In some languages, you could define a new division operator that returns an Optional/Option/Maybe result, and then use a nil-coalescing operator to choose what you want in case of division-by-zero. In Swift:

    infix operator /? : MultiplicationPrecedence
    extension FloatingPoint {
        static func /? (lhs: Self, rhs: Self) -> Self? {
            return rhs == 0 ? nil : lhs / rhs
        }
    }
    extension BinaryInteger {
        static func /? (lhs: Self, rhs: Self) -> Self? {
            return rhs == 0 ? nil : lhs / rhs
        }
    }

    10 /? 2 ?? 0  // -> 5
    10 /? 0 ?? 0  // -> 0
    10 /? 0 ?? 10 // -> 10



You can't define your own in Pony but,

/?, *?, +? and -? are coming to Pony. We call them "safe math operators" and will error on integer overflow, underflow, and division by zero.




Applications are open for YC Winter 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: