Function Overloading
์ค๋ฒ๋ก๋ฉ์ ์ง์๋์ง ์์ต๋๋ค:
- ๊ฐ๋ณํจ์๋ ๋จ์ผ ๊ตฌํ๋ง ๊ฐ์ต๋๋ค.
- ํญ์ ๊ณ ์ ๋ ์์ ๋งค๊ฐ๋ณ์๋ง ๊ฐ์ต๋๋ค.
- ํญ์ ๊ณ ์ ๋ ๋จ์ผ ํ์ ์งํฉ์ ์ฌ์ฉํฉ๋๋ค.
- ๋งค๊ฐ๋ณ์์ ๊ธฐ๋ณธ ๊ฐ์ ์ง์๋์ง ์์ต๋๋ค.
- ๋ชจ๋ ํธ์ถ๋ถ์์๋ ๋์ผํ ์์ ์ธ์๋ฅผ ์ค์ ํด์ผํฉ๋๋ค.
- ๋์์ผ๋ก ๋งคํฌ๋ก๋ฅผ ์ฌ์ฉํ๊ธฐ๋ ํฉ๋๋ค.
ํ์ง๋ง, ํจ์์ ๋งค๊ฐ๋ณ์๋ ์ ๋๋ฆญ์ ์ ์ฉํ ์ ์์ต๋๋ค.
Overloading is not supported:
- Each function has a single implementation:
- Always takes a fixed number of parameters.
- Always takes a single set of parameter types.
- Default values are not supported:
- All call sites have the same number of arguments.
- Macros are sometimes used as an alternative.
However, function parameters can be generic:
// ์ ๋๋ฆญ fn pick_one<T>(a: T, b: T) -> T { if std::process::id() % 2 == 0 { a } else { b } } fn main() { println!("coin toss: {}", pick_one("heads", "tails")); println!("cash prize: {}", pick_one(500, 1000)); }
์ญ์ฃผ
- std::process::id: OS๊ด๋ จ ํ๋ก์ธ์ค ID๋ฅผ ๋ฐํํ๋ ํจ์์ ๋๋ค.
- for js developer:
::
ํํ์ ์ฝ๊ฒ ๋งํด static ํจ์ ํธ์ถ์ ๋๋ค.