Skip to content

Instantly share code, notes, and snippets.

@goofmint
Last active April 14, 2017 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goofmint/45e3fa6dd8d92dedbf5d285ff9feea67 to your computer and use it in GitHub Desktop.
Save goofmint/45e3fa6dd8d92dedbf5d285ff9feea67 to your computer and use it in GitHub Desktop.
2.md
/**
* A reference must be explicitly marked as nullable to be able hold a null.
* See http://kotlinlang.org/docs/reference/null-safety.html#null-safety
*/
package multiplier
// Return null if str does not hold a number
fun parseInt(str: String): Int? {
try {
return str.toInt()
} catch (e: NumberFormatException) {
println("One of the arguments isn't Int")
}
return null
}
fun main(args: Array<String>) {
if (args.size < 2) {
println("No number supplied");
} else {
val x = parseInt(args[0])
val y = parseInt(args[1])
// We cannot say 'x * y' now because they may hold nulls
if (x != null && y != null) {
print(x * y) // Now we can
} else {
println("One of the arguments is null")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment