Type Inference
-
[Swift] Type Inference, Type AnnotationSwift 2022. 1. 5. 23:04
(๋ชจ๋ ์ฝ๋๋ Playground์์ ์์ฑ๋์์ต๋๋ค.) Swift์์ ๋ณ์ ๋๋ ์์๋ฅผ ์ ์ธํ๋ ๋ฐฉ๋ฒ์ผ๋ก๋ ํฌ๊ฒ 2๊ฐ์ง๊ฐ ์๋ค. ๋ฐ๋ก Type Inference๊ณผ Type Annotation์ธ๋ฐ, ๋ง ๊ทธ๋๋ก Type Inference๋ ์๋ฃํ์ ์ถ๋ก ํ๋ ๊ฒ์ด๊ณ , Type Annotation์ ์๋ฃํ์ ์ง์ ์ง์ ํด์ฃผ๋ ๊ฒ์ด๋ค. ์ด์ ๋ํด ์์ธํ ์์๋ณด๋๋ก ํ์. Type Inference Type Inference๋ ํ์ ์ ์ถ๋ก ํ๋ ๊ฒ์ผ๋ก, ์ด๊ธฐ๊ฐ์ ํตํด ์๋ฃํ์ด ๊ฒฐ์ ๋๋ ๊ฒ์ด๋ค. ์ฆ, ์๋ฃํ์ ์ง์ ์ง์ ํด์ฃผ์ง ์๋๋ค. ๋ฐ๋ผ์ Type Inference๋ฅผ ์ด์ฉํ๋ ค๋ฉด ๋ฐ๋์ ์ด๊ธฐ๊ฐ์ ์ ์ฅํด์ฃผ์ด์ผ ํ๋ค. ์๋์ ๊ฐ์ด var ๋ณ์์ด๋ฆ = ์ด๊ธฐ๊ฐ ๋๋ let ์์์ด๋ฆ = ์ด๊ธฐ๊ฐ ์ผ๋ก ๋ณ์๋ฅผ ์ ์ธํ๋ฉด, Type I..