Division operator (/) and int.divide have different behavior. #4733
-
When dividing by 0 using the division operator, it will return 0. When dividing by 0 using the int.divide() function, it will return a Result. According to this section in the FAQ, this is intentional. However this inconsistency caused a problem for me when creating a generic print function. The function was a helper for a programming challenge/exercise I was working on... here's the code fn exercise05() {
let first_num =
read_line_retry(
"What is the first number? ",
"Please enter a valid integer...",
int.parse,
)
let second_num =
read_line_retry(
"What is the second number? ",
"Please enter a valid integer...",
int.parse,
)
let first_num_string = int.to_string(first_num)
let second_num_string = int.to_string(second_num)
// io.println(
// first_num_string
// <> " + "
// <> second_num_string
// <> " = "
// <> int.to_string(first_num + second_num),
// )
// io.println(
// first_num_string
// <> " - "
// <> second_num_string
// <> " = "
// <> int.to_string(first_num - second_num),
// )
// io.println(
// first_num_string
// <> " * "
// <> second_num_string
// <> " = "
// <> int.to_string(first_num * second_num),
// )
// io.println(
// first_num_string
// <> " / "
// <> second_num_string
// <> " = "
// <> int.to_string(first_num / second_num),
// )
print_arith(first_num, second_num, "+", int.add, int.to_string)
print_arith(first_num, second_num, "-", int.subtract, int.to_string)
print_arith(first_num, second_num, "*", int.multiply, int.to_string)
print_arith(first_num, second_num, "/", int.divide, int.to_string) // type error!
} fn print_arith(
first: a,
second: a,
arith_str: String,
op: fn(a, a) -> a,
to_string: fn(a) -> String,
) {
let cleaned_arith_str =
arith_str
|> string.trim
|> string.pad_end(2, " ")
|> string.pad_start(3, " ")
let result = op(first, second)
io.println(
to_string(first)
<> cleaned_arith_str
<> to_string(second)
<> " = "
<> to_string(result),
)
} As you can see, the point was to create this generic print function to avoid boilerplate... really what I wanted to do was to pass the operators themselves into the function, but I suppose that is not possible? Here are my questions:
I'm not poo-pooing the language... I understand and appreciate very much the principled approach to keeping the language small, simple and functional. I've been enjoying the language for the most part but I've felt a bit limited in some things and I'm not sure what the proper/idiomatic way is to address these things. I appreciate any help in understanding how to view the language :) Thanks for your consideration. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hopefully this answers some of your questions. Let me know if you have further doubts |
Beta Was this translation helpful? Give feedback.
int.divide
now. This would be quite a large breaking change and break many existing packages. It's meant to be a different API from the division operator, so that you can either have absolute safety and get anError
when the division fails, or not have to think about it and just receive zero.fn(a, b) { a / b }
strin…