Search code examples
icon-language

How does success/failure in Icon differ from boolean values in other languages?


I have some difficulties to understand the concept of failure and success in the Icon programming language. See the highlighted part below from a tutorial for Icon https://www2.cs.arizona.edu/icon/docs/ipd266.htm. How does success/failure in Icon differ from boolean values in other languages?

enter image description here


Solution

  • If find returned a boolean signifying success/failure, it wouldn't be able to also return "the position at which "or" occurs in line". In languages like C, this can be worked around by taking and modifying an out parameter, but this is awkward and error-prone. In languages with algebraic datatypes, you could have the function return something like Option<Int>. Icon encodes "optionality" and the corresponding control flow on the language level – not only can any function fail, but control structures (like if) work by detecting and handling failures. (This distinguishes it from languages where values are "nullable", but null values simply crash the program or cause an exception to be thrown.)