String to Int in java – Likely bad data, need to avoid exceptions

I asked if there were open source utility libraries that had methods to do this parsing for you and the answer is yes! From Apache Commons Lang you can use NumberUtils.toInt: // returns defaultValue if the string cannot be parsed. int i = org.apache.commons.lang.math.NumberUtils.toInt(s, defaultValue); From Google Guava you can use Ints.tryParse: // returns null … Read more

What protocol should be adopted by a Type for a generic function to take any number type as an argument in Swift?

Update: The answer below still applies in principle, but Swift 4 completed a redesign of the numeric protocols, such that adding your own is often unnecessary. Take a look at the standard library’s numeric protocols before you build your own system. This actually isn’t possible out of the box in Swift. To do this you’ll … Read more