price + (10.percent * 2) looks like 12: If 10.percent = 1, 1*2 = 2. 10 + 2 = 12.
Under the usual rules of operator precedence, that would be the expected answer (multiplication having higher precedence than addition).
Under the usual rules of operator precedence, that would be the expected answer (multiplication having higher precedence than addition).
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.