M
Modular12mo ago
Mr_Dojo0

Why is overloading a function with different types not possible:

fn sum(num1: Int, num2: Int) -> int: return num1 + num2 fn sum(num1: Float, num2: Float) -> Float: return num1 + num2
1 Reply
TeamPuzel
TeamPuzel12mo ago
Your code is incorrect, there is no int, only Int and there's Float32 not Float function overloading is possible.
fn sum(lhs: Int, rhs: Int) -> Int:
return lhs + rhs
fn sum(lhs: Float32, rhs: Float32) -> Float32:
return lhs + rhs

fn main():
let float: Float32 = 1
let int: Int = 2

print(sum(float, float))
print(sum(int, int))
fn sum(lhs: Int, rhs: Int) -> Int:
return lhs + rhs
fn sum(lhs: Float32, rhs: Float32) -> Float32:
return lhs + rhs

fn main():
let float: Float32 = 1
let int: Int = 2

print(sum(float, float))
print(sum(int, int))
will print
2.0
4
2.0
4
Want results from more Discord servers?
Add your server