Re:Kotlin Money
by bloated5048 on Tuesday October 08, 2024 @11:16PM
(#41784169)
How does computer know it's 10% of 10? Unless specified, 10% is 0.1
Or am I an idiot and missing something here
If you oveload "+" to mean whatever-you-want, it's possible. Using bad python [1], it's something like:
def sum(x,y):
if is_number(x) and is_number(y):
return x + y
elif is_number(x) and is_percent(y):
return x * (1 + y.value/100)
else:
raise fatal_error
I don't claim it's a good idea, but it's possible if the language allows operator overload.[1] Sorry, my main current language is Racket, but I thought that python-like is easier to read for most people, in spite people that likes python can find like 10 error in 7 LOC.
Under the usual rules of operator precedence would be the expected answer (multiplication having higher precedence than addition).