Secret-Of-SwiftUI / SSDC22

๐Ÿคก ์‘~ ์งˆ๋ ค? deprecated ํ•˜๋ฉด ๊ทธ๋งŒ์ด์•ผ~
9 stars 0 forks source link

Embrace Swift generics #8

Open hyun99999 opened 2 years ago

hyun99999 commented 2 years ago

Generics are a fundamental tool for writing abstract code in Swift. Learn how you can identify opportunities for abstraction as your code evolves, evaluate strategies for writing one piece of code with many behaviors, and discover language features in Swift 5.7 that can help you make generic code easier to write and understand.

hyun99999 commented 2 years ago

Abstraction separated ideas from specific details

function ์—์„œ ๊ธฐ๋Šฅ์„ฑ์„ ์ถ”์ถœํ•  ๋•Œ, details ์€ abstracted ๋กœ ๋ถ€ํ„ฐ ๋ฉ€์–ด์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  abstraction ์€ details ์„ ๋ฐ˜๋ณต์—†์ด ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๊ณ  ์žˆ๋Š”์ง€ ์•„์ด๋””์–ด๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์Šคํฌ๋ฆฐ์ƒท 2022-06-18 ์˜คํ›„ 3 23 51

Swift ์—์„œ๋Š” concrete type ์„ abstract ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2

๋ฐ”๋กœ ์ด๊ฒƒ..! ์ฒ˜๋Ÿผ์š”

์„œ๋กœ ๋‹ค๋ฅธ details ๋ฅผ ๊ฐ€์ง„ ๋™์ผํ•œ ์•„์ด๋””์–ด์˜ set of types ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ abstract code ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ concrete types ์œผ๋กœ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ๋‹ค์Œ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

farm ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค!

๐Ÿ‘‰ย Model with concrete types

์•„๋ž˜๋Š” Cow ๋ผ๋Š” ๊ตฌ์กฐ์ฒด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ง , ์ด์ œ ์šฐ๋ฆฌ๋Š” cow ๋“ค์„ farm ์—์„œ ๊ธฐ๋ฅผ ์ˆ˜ ์žˆ์–ด์š”.

ํ•˜์ง€๋งŒ ์ข€ ๋” ๋งŽ์€ ๋™๋ฌผ๋“ค์„ ์›ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”?

feed ๋ฉ”์„œ๋“œ๋ฅผ overload ํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ๊ฐ ๊ตฌํ˜„์€ ๋งค์šฐ ํก์‚ฌํ•˜๊ณ , ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋˜์–ด๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰ย Identify common capabilities

๋‹ค์Œ์€ ๋™๋ฌผ ํƒ€์ž…๋“ค ์‚ฌ์ด์˜ common capabilities ๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋™๋ฌผ๋“ค์€ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋จน์ด๋ฅผ ๋จน๊ณ , eat ๋ฉ”์„œ๋“œ๋Š” ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„๋˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ abstract code ๊ฐ€ eat ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ํ•˜๊ณ , concrete type ์— ๋”ฐ๋ผ abstract code ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด์ฒ˜๋Ÿผ ๋‹ค๋ฅธ concrete types ์— ๋Œ€ํ•ด ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๋Š” abstract code ๋ฅผ polymorphism ์ฆ‰, ๋‹คํ˜•์„ฑ ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹คํ˜•์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ ์‚ฌ์šฉ ๋ฐฉ์‹์— ๋”ฐ๋ผ์„œ ํ•˜๋‚˜์˜ ์ฝ”๋“œ ์กฐ๊ฐ์ด ์—ฌ๋Ÿฌ ๋™์ž‘์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹คํ–ฅ์„ฑ์€ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ๋Š” overloading ์œผ๋กœ, ๋™์ผํ•œ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด argument ์˜ ํƒ€์ž…์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฒ„๋กœ๋”ฉ์€ ์ผ๋ฐ˜์ ์ธ ์†”๋ฃจ์…˜์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ad-hoc polymorphism(์ž„์‹œ ๋‹คํ–ฅ์„ฑ)์ด๋ผ๊ณ  ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์•ž์„œ ์˜ค๋ฒ„๋กœ๋”ฉ์œผ๋กœ ๋ฐ˜๋ณต์ ์ธ ์ฝ”๋“œ๋กœ ์ด์–ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ๋Š” subtype ์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ, generic ์„ ์‚ฌ์šฉํ•˜๋Š” parametric polymorphism ์ด ์žˆ์Šต๋‹ˆ๋‹ค. type parameters ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ํƒ€์ž…๊ณผ ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ฉฐ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์ด arguments ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์ด๋ฏธ overloads ๋ฅผ ๋ฐฐ์ œํ–ˆ์œผ๋ฏ€๋กœ subtype polymorphism ์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. subtype ๊ด€๊ณ„์˜ ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ•์€ class hierarchy ์ž…๋‹ˆ๋‹ค.

Animal ์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ ๋™๋ฌผ ํด๋ž˜์Šค์—์„œ Animal ์„ ์ƒ์†๋ฐ›์•„์„œ eat ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, subtype polymorphism ์„ ์‚ฌ์šฉํ•ด์„œ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ์˜ ๊ตฌํ˜„์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

Animal ์˜ eat ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…์„ ์•„์ง ์ฑ„์šฐ์ง€ ๋ชปํ–ˆ์œผ๋ฉฐ, ์ด ์ฝ”๋“œ์—๋Š” ๋ช‡๊ฐ€์ง€ red flags! ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ—˜ ์‹ ํ˜ธ๊ฐ€ ์žˆ๋‹ค๋Š” ์†Œ๋ฆฌ์ฃ ..

์šฐ๋ฆฌ๊ฐ€ ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ์‹์€ Any ์™€ ๊ฐ™์ด ๋œ ๊ตฌ์ฒด์ ์ธ ์œ ํ˜•์„ ํ—ˆ์šฉํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, ์ด ๋ฐฉ๋ฒ•์€ ์œ„์™€ ๊ฐ™์ด ์˜ฌ๋ฐ”๋ฅธ ํƒ€์ž…์ด ์ „๋‹ฌ๋˜์—ˆ๋Š”์ง€ as? ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์šด์บ์ŠคํŒ…์„ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ถ”๊ฐ€์ ์ธ boilerplate code ๋ฅผ ๋ถ€๊ณผํ–ˆ์ง€๋งŒ ์‹ค์ˆ˜๋กœ ์ž˜๋ชป๋œ ํƒ€์ž…์„ ์ง‘์–ด๋„ฃ์—ˆ์„ ๋•Œ๋Š” ๋Ÿฐํƒ€์ž„์—๋งŒ ์žก์„ ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฒ„๊ทธ๋ฅผ ๋‚จ๊ธธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ž! ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ•ด ๋ด…์‹œ๋‹ค.

type parameter(ํƒ€์ž… ์ธ์ž. ex. T) ๋ฅผ ๋„์ž…ํ•ด์„œ type-safe ๋ฐฉ๋ฒ•์œผ๋กœ ๋™๋ฌผ์˜ food ์˜ ํƒ€์ž…์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ Food ํƒ€์ž… ์ธ์ž๊ฐ€ Animal ํด๋ž˜์Šค์˜ ์„ ์–ธ์œผ๋กœ ์Šน๊ฒฉ๋˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋ถ€์ž์—ฐ์Šค๋Ÿฝ์Šต๋‹ˆ๋‹ค. ๋™๋ฌผ์ด ๋™์ž‘ํ•˜๋ ค๋ฉด ์Œ์‹์ด ํ•„์š”ํ•˜์ง€๋งŒ ์Œ์‹์„ ๋จน๋Š” ๊ฒƒ์ด ๋™๋ฌผ์˜ ํ•ต์‹ฌ ๋ชฉ์ ์ด ์•„๋‹ˆ๋ฉฐ ๋™๋ฌผ์—์„œ ์ž‘๋™ํ•˜๋Š” ๋งŽ์€ ์ฝ”๋“œ๋“ค์€ ์Œ์‹์— ๋Œ€ํ•ด์„œ ์ „ํ˜€ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๋„๋•๋„๋•..

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒŒ ๋˜๋ฉด Animal ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋ชจ๋“  ์ฐธ์กฐ๋Š” food ํƒ€์ž…์„ ์ง€์ •ํ•ด์•ผ๋งŒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ, ํ•„์š”ํ•˜๋‹ค๋ฉด ๊ฐ Animal ์— ํƒ€์ž…์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ์œ„์˜ ๋ฐฉ๋ฒ• ์ค‘ ์–ด๋Š ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ์‹์€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์ž‘๋™ ๋ฐฉ์‹์— ๋Œ€ํ•œ ์„ธ๋ถ€์ •๋ณด ์—†์ด capabilites ์˜ ํƒ€์ž…์„ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค!

๋™๋ฌผ์—๊ฒŒ๋Š” ๋‘ ๊ฐ€์ง€ capabilites ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ ์œ„์˜ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.

๐Ÿ‘‰ย Build an interface

Swift ์—์„œ๋Š” protocol ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœํ† ์ฝœ์€ ์ค€์ˆ˜ํ•˜๋Š” ํƒ€์ž…์˜ ๊ธฐ๋Šฅ์„ ์„ค๋ช…ํ•˜๋Š” ์ถ”์ƒํ™” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.

A protocol separated ideas from implementation details.

15

type parameter ์ฒ˜๋Ÿผ associated type ์€ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์— ๋Œ€ํ•œ placeholder ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

associated type ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ์จ ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๋Š” ํŠน์ • ํƒ€์ž…์— ์˜์กดํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํŠน์ • ํƒ€์ž…์˜ Animal ์˜ ๊ฐ ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐ™์€ food ํƒ€์ž…์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.

16

๋‹ค์Œ์œผ๋กœ ์Œ์‹์„ ์†Œ๋น„ํ•˜๋Š” ์ž‘์—…์€ eat ๋ฉ”์„œ๋“œ์— ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค. Animal ์˜ Feed ํƒ€์ž…์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

ํ”„๋กœํ† ์ฝœ์—๋Š” ๋ฉ”์„œ๋“œ์˜ ๊ตฌํ˜„์ด ๋˜์–ด์žˆ์ง€ ์•Š์œผ๋ฉฐ, ๊ตฌํ˜„ํ•˜๋ ค๋ฉด concrete Animal ํƒ€์ž…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

17

ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ Animal ํƒ€์ž…์€ eat ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋ฉฐ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” Feed ํƒ€์ž…์„ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์šฐ๋ฆฌ๋Š” ๋™๋ฌผ์˜ common capabilites ๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ์‹๋ณ„ํ•˜๊ณ , ํ”„๋กœํ† ์ฝœ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Šฅ๋ ฅ์„ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ generic code ๋ฅผ ์‚ฌ์šฉํ•ด๋ด…์‹œ๋‹ค.

๐Ÿ‘‰ย Write generic code

์šฐ๋ฆฌ๋Š” ๋ชจ๋“  concrete Animal ํƒ€์ž…์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š” ํ•˜๋‚˜์˜ ๊ตฌํ˜„์„ ๋งŒ๋“ค๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค! parametric polymorphism ์„ ์‚ฌ์šฉํ•˜๊ณ  ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ type parameter ๋ฅผ ๋„์ž…ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

18

๊ทธ๋ฆฌ๊ณ  ํ•ญ์ƒ concrete Animal(๊ตฌํ˜„๋œ Animal. ์ฆ‰, Animal ์„ ์ค€์ˆ˜ํ•œ ๊ฒƒ.)๊ฐ€ Animal ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋ฏ€๋กœ <A: Animal> ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๋Š” ๊ฒƒ์€ ๊บฝ์‡  ๊ด„ํ˜ธ๋กœ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ trailing where ์ ˆ์— ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‹ค๋ฅธ type parameters ์™€์˜ ๊ด€๊ณ„๋„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

19
func feed<A>(_ animal: A) where A: Animal

์œ„์˜ ์ œ๋„ค๋ฆญ ํŒจํ„ด์€ ์‹ค์ œ๋ณด๋‹ค ํ›จ์”ฌ ๋ณต์žกํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

func feed(_ animal: some Animal)

ํƒ€์ž… ์ธ์ž๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋Œ€์‹  some Animal ์„ ์ž‘์„ฑํ•˜์—ฌ protocol conformance ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ ์„ ์–ธ๊ณผ ๋™์ผํ•˜์ง€๋งŒ ๋ถˆํ•„์š”ํ•œ ํƒ€์ž… ์ธ์ž ๋ชฉ๋ก๊ณผ where ์ ˆ์ด ์‚ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค.

some

some Animal ์—์„œ some ์€ ์ž‘์—… ์ค‘์ธ ํŠน์ • ํƒ€์ž…์ด ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. some ํ‚ค์›Œ๋“œ ๋’ค์—๋Š” ํ•ญ์ƒ conformance ์š”๊ตฌ์‚ฌํ•ญ์ด ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ํŠน์ • ํƒ€์ž…์€ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•ด Animal ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•ด์•ผํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

some ํ‚ค์›Œ๋“œ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ result ํƒ€์ž…์—๋„ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

var body: some View { ... }
var body: modifiercontent<text, _backgroundmodifier<color>> {

}

SwiftUI ์ฝ”๋“œ์—์„œ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. body ํ”„๋กœํผํ‹ฐ๋Š” ํŠน์ • View ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, ์ฝ”๋“œ๋Š” ํŠน์ • ํƒ€์ž…์ด ๋ฌด์—‡์ธ์ง€ ์•Œ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ concrete type ์— ๋Œ€ํ•œ placeholder ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” abstract type ์ด ๋ฐ”๋กœ opaque type, ๋ถˆํˆฌ๋ช… ํƒ€์ž… ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋ทฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ํƒ€์ž…์œผ๋กœ body ๋ฅผ ์ •์˜ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ์ด๊ฒƒ์ด ๋ฐ”๋กœ opaque type ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ถˆํˆฌ๋ช… ํƒ€์ž…์€ protocol type ์— ๋น„ํ•ด์„œ ๊ฐ•๋ ฅํ•œ ๋ณด์žฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒ๋ฉด protocol type ์€ ํŠน์ • ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๊ณ  ์žˆ๊ธฐ๋งŒํ•˜๋‹ค๋ฉด mulitple type ์— ๋Œ€ํ•ด์„œ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, opaque type ์€ single type ์ด์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ :

[Swift] Opaque Type vs Protocol Type


์‹ค์ œ ์‚ฌ์šฉ๋˜๋Š” ํƒ€์ž…์„ underlying type ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ’์— ์ ‘๊ทผํ•  ๋•Œ๋งˆ๋‹ค ๋™์ผํ•œ underlying type ์„ ๊ฐ€์ ธ์˜ค๋„๋ก ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค.

20

โ†’ (function arrow) ๋Š” parameter position ๊ณผ result position ์„ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.

opaque type ์˜ ์œ„์น˜๋กœ ํ”„๋กœ๊ทธ๋žจ์˜ ์–ด๋Š ๋ถ€๋ถ„์ด abstract trype ์„ ๋ณด๊ณ  concrete type ์„ ์ •ํ•˜๋Š”์ง€ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

21

๋ช…์‹œ๋˜๋Š” ํƒ€์ž… ์ธ์ž๋Š” input ์ธก์—์„œ ์„ ์–ธ๋˜๊ณ , ํ˜ธ์ถœ์ž๊ฐ€ underlying type ์„ ๊ฒฐ์ •ํ•˜๊ณ  ๊ตฌํ˜„์€ abstract type ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ opaque type ๋˜๋Š” result type ๋Œ€ํ•œ ๊ฐ’์„ ์ œ๊ณตํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๋ถ€๋ถ„์€ underlying type ์„ ๊ฒฐ์ •ํ•˜๊ณ , ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๋ถ€๋ถ„์€ abstract type ์„ ๋ด…๋‹ˆ๋‹ค.

22

local variable ์— ๋Œ€ํ•ด์„œ underlying type ์€ ํ• ๋‹น์˜ right-hand side ์—์„œ ์œ ์ถ”๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ opaque type ์˜ ์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ํ•ญ์ƒ ์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ€์ ธ์•ผํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฉด, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” error ์„ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ, underlying type ์€ ํ•ด๋‹น ๋ณ€์ˆ˜์˜ scope ์— ๋Œ€ํ•ด์„œ๋Š” ๊ณ ์ •๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ underlying type ์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด error ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

23

opaque type ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฒฝ์šฐ, underlying type ์€ ํ˜ธ์ถœ ๋ถ€๋ถ„์˜ argument ๋กœ ๋ถ€ํ„ฐ ์œ ์ถ”๋ฉ๋‹ˆ๋‹ค.

some ์„ ํŒŒ๋ผ๋ฏธํ„ฐ ํฌ์ง€์…˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด Swift 5.7 ์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

underlying type ์€ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฒ”์œ„ ๋‚ด์—์„œ๋งŒ ์ˆ˜์ •๋˜๋ฏ€๋กœ ๊ฐ ํ˜ธ์ถœ์€ ๋‹ค๋ฅธ argument type ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

24

opaque type ์˜ result type ์˜ ๊ฒฝ์šฐ, underlying type ์ด ๊ตฌํ˜„์˜ ๋ฐ˜ํ™˜๊ฐ’์—์„œ ์œ ์ถ”๋ฉ๋‹ˆ๋‹ค.

opaque result ๋ฅผ ๊ฐ€์ง„ ๋ฉ”์„œ๋“œ ํ˜น์€ computed property ๋Š” ํ”„๋กœ๊ทธ๋žจ ์–ด๋””์—์„œ๋‚˜ ํ˜ธ์ถœ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ช…๋ช…๋œ ๊ฐ’์˜ ๋ฒ”์œ„๋Š” ์ „์—ญ์ž…๋‹ˆ๋‹ค. ์ด๋Š” underlying return type ์ด ๋ชจ๋“  return ๋ฌธ์—์„œ ๋™์ผํ•ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

25

์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” underlying ๊ฐ’์ด ๋ฏธ์Šค๋งค์นญ๋˜์—ˆ๋‹ค๊ณ  ์˜ค๋ฅ˜๋ฅผ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค.

26

opaque SwiftUI view ์˜ ๊ฒฝ์šฐ, ViewBuilder ์€ ๊ฐ ๋ถ„๊ธฐ์— ๋Œ€ํ•ด ๋™์ผํ•œ underlying return type ์„ ๊ฐ–๋„๋ก control-flow ๋ฌธ์„ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ViewBuilder ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋“œ๋””์–ด.. feed ๋ฉ”์„œ๋“œ๋กœ ๋Œ์•„๊ฐ€ ๋ด…์‹œ๋‹ค.

27

๋‹ค๋ฅธ ๊ณณ์—์„œ opaque type ์„ ์ฐธ์กฐํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฆฌ์ŠคํŠธ์—์„œ some ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜์—์„œ opaque type ์„ ์—ฌ๋Ÿฌ๋ฒˆ ์ฐธ์กฐํ•ด์•ผํ•  ๋•Œ๊ฐ€ ํƒ€์ž… ์ธ์ž๊ฐ€ ์œ ์šฉํ•  ๋•Œ์ž…๋‹ˆ๋‹ค.

28

์˜ˆ๋ฅผ ๋“ค์–ด, Habitat ๋ผ๋Š” ๋‹ค๋ฅธ associatedtype ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ๋†์žฅ์— ์ง€์ •๋œ ๋™๋ฌผ์˜ ์„œ์‹์ง€๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ๋ฅผ ์›ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, result type ์€ ํŠน์ • ๋™๋ฌผ ํƒ€์ž…์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋ฆฌํ„ด ํƒ€์ž…์— ํƒ€์ž… ์ธ์ž A ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

29

opaque tpye ์„ ์—ฌ๋Ÿฌ๋ฒˆ ์ฐธ์กฐํ•ด์•ผํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ์ƒํ™ฉ์€ generic type ์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ๋Š” ์ข…์ข… ํƒ€์ž…์ธ์ž๋ฅผ ์„ ์–ธํ•˜๊ณ , ํƒ€์ž…์ธ์ž๋ฅผ ์ €์žฅ ํ”„๋กœํผํ‹ฐ์— ์‚ฌ์šฉํ•˜๊ณ , ๋‹ค์‹œ memberwise initializer ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ context ์—์„œ ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ์ฐธ์กฐํ•˜๋ ค๋ฉด < > ์•ˆ์— ํƒ€์ž… ์ธ์ž๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ๋ช…ํ™•ํžˆ ํ•˜๋Š”๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ feed ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด๋ด…์‹œ๋‹ค.

30

animal ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ grow ํ•˜๋Š” ์ž‘๋ฌผ ํƒ€์ž…์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ, harvest ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ž‘๋ฌผ์„ ์ˆ˜ํ™•ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  animal ์—๊ฒŒ ๋จน์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

underlying animal type ์ด ๊ณ ์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์—์„œ ํƒ€์ž…์˜ ๊ด€๊ณ„์— ๋Œ€ํ•ด์„œ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

31

์ด๋Ÿฌํ•œ ์ •์  ๊ด€๊ณ„๋Š” ๋™๋ฌผ์—๊ฒŒ ์ž˜๋ชป๋œ ํƒ€์ž…์˜ ๋จน์ด๋ฅผ ๋จน์ด๋Š” ์‹ค์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.

๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” feedAll ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. element ํƒ€์ž…์ด Animal ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์€ ์•Œ๊ณ ์žˆ์ง€๋งŒ ๋ฐฐ์—ด์ด ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ ๋™๋ฌผ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ธฐ๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค.

32

some ์—๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” underlying type ์ด ์žˆ์Šต๋‹ˆ๋‹ค. underlying type ์€ ๊ณ ์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋Š” ๋™์ผํ•œ ํƒ€์ž…์„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ some Animal ์€ ์›ํ•˜๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” ๋ชจ๋“  ์œ ํ˜•์˜ Animal ์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” supertype ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

any Animal ์‚ฌ์šฉํ•˜๋ฉด arbitrary type ์˜ Animal ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ storage flexibility ์„ ํ—ˆ๋ฝํ•˜๊ธฐ ์œ„ํ•ด์„œ any Animal ํƒ€์ž…์€ ๋ฉ”๋ชจ๋ฆฌ์— ํŠน๋ณ„ํ•œ ํ‘œํ˜„์ด ์žˆ์Šต๋‹ˆ๋‹ค.

33

๊ฐ’์ด ์ƒ์ž ์•ˆ์— ์ง์ ‘ ๋“ค์–ด๊ฐˆ ๋งŒํผ ์ถฉ๋ถ„ํžˆ ์ž‘๊ธฐ๋„ ํ•˜์ง€๋งŒ ๋•Œ๋ก  ์ƒ์ž์— ๋น„ํ•ด ๋„ˆ๋ฌด ์ปค์„œ ๋‹ค๋ฅธ ๊ณณ์— ํ• ๋‹นํ•˜๊ณ  ํ•ด๋‹น ๊ฐ’์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

any Animal ํƒ€์ž…์€ ๋™์ ์œผ๋กœ ๋ชจ๋“  concrete Animal type ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ณ , existential type ์ด๋ผ๊ณ  ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ concrete types ์— ๋Œ€ํ•ด ๋™์ผํ•œ ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ type erasure ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. concrete type ์€ ์ปดํŒŒ์ผ ํƒ€์ž„์— erase ๋˜๊ณ  ๋Ÿฐํƒ€์ž„์—๋งŒ ์•Œ๋ ค์ง‘๋‹ˆ๋‹ค.

extential type any Animal ์˜ ๋‘ ์ธ์Šคํ„ด์Šค๋Š” ์ •์  ํƒ€์ž…์€ ๋™์ผํ•˜์ง€๋งŒ ๋™์  ํƒ€์ž…์€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. type erasure ๋Š” ๋‹ค๋ฅธ ๋™๋ฌผ ๊ฐ’ ์‚ฌ์ด์˜ type-level distinction ์„ ์ œ๊ฑฐํ•˜์—ฌ ๋‹ค๋ฅธ ๋™์  ์œ ํ˜•์˜ ๊ฐ’์„ ๋™์ผํ•œ ์ •์  ์œ ํ˜•์œผ๋กœ ๊ตํ™˜ ๊ฐ€๋Šฅํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

type erasure ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๋ฐฐ์—ด์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

34

๋‹ค์Œ๊ณผ ๊ฐ™์ด associated types ๊ฐ€ ์žˆ๋Š” ํ”„๋กœํ† ์ฝœ์— any ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด Swift 5.7 ์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

35

feedAll ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ anmial ์— ๋Œ€ํ•ด์„œ eat ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ฐ˜๋ณต์—์„œ underlying animal ์— ๋Œ€ํ•œ Feed ํƒ€์ž…์„ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Animal ์— ๋Œ€ํ•ด eat ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๊ณ  ํ•˜์ž ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

36

type-level distinction ์„ ์ œ๊ฑฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํŠน์ • animal types ์— ๋Œ€ํ•œ ์˜์กดํ•˜๋Š” associated types ๋„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ์ด ๋™๋ฌผ์ด ์–ด๋–ค ๋จน์ด๋ฅผ ์˜ˆ์ƒํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ! ์šฐ๋ฆฌ๋Š” type relationships ๋ฅผ ์˜์กดํ•˜๋ ค๋ฉด ํŠน์ • ํƒ€์ž…์˜ ๋™๋ฌผ์ด ๊ณ ์ •๋œ context ๋กœ ๋Œ์•„๊ฐ€์•ผ ํ•ฉ๋‹ˆ๋‹ค. Animal ์—์„œ ์ง์ ‘ eat ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋Œ€์‹  some Animal ์„ ์‚ฌ์šฉํ•˜๋Š” feed ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

37

any Animal ๊ณผ some Animal ์€ ๋‹ค๋ฅธ ํƒ€์ž…์ด์ง€๋งŒ, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” underlying value ๋ฅผ unboxing ํ•˜๊ณ , some Animal ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ง์ ‘ ์ „๋‹ฌํ•จ์œผ๋กœ์จ any Animal ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ some Animal ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

arguments ๋ฅผ unboxing ํ•˜๋Š” ๊ฒƒ์ด Swift 5.7 ์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

some Animal ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฒ”์œ„์˜ ๊ฒฝ์šฐ, ๊ฐ’์ด underlying type ์œผ๋กœ ๊ณ ์ •๋˜์žˆ์œผ๋ฏ€๋กœ underlying type ์— ๋Œ€ํ•œ operations ๋ฅผ ํฌํ•จํ•˜์—ฌ associated types ์—๋„ ์•ก์„ธ์Šค ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

38

์ด๊ฒƒ์€ ์ •์  ํƒ€์ž…์˜ ํ‘œํ˜„์„ฑ์„ ๊ฐ–๋Š” ์ปจํ…์ŠคํŠธ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. ์œ„์˜ some ์„ ์‚ฌ์šฉํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ๊ฐ ๋™๋ฌผ์„ feed ๋ฉ”์„œ๋“œ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฐ ๋ฐ˜๋ณต์—์„œ ํŠน์ • ๋™๋ฌผ์—๊ฒŒ ๋จน์ผ ์ ์ ˆํ•œ ์ž‘๋ฌผ์„ ์žฌ๋ฐฐํ•˜๊ณ  ์ˆ˜ํ™•ํ•˜์—ฌ ๋จน์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ณผ์ •์—์„œ some ๊ณผ any ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

39

some

some ์„ ์‚ฌ์šฉํ•˜๋ฉด underlying type ์ด ๊ณ ์ •๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด generic code ์˜ underlying type ์— ๋Œ€ํ•œ type relationships ๋ฅผ ์˜์กดํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ž‘์—…์ค‘์ธ ํ”„๋กœํ† ์ฝœ์˜ API ๋ฐ associated types ์— ๋Œ€ํ•œ ์ „์ฒด ์•ก์„ธ์Šค ๊ถŒํ•œ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

any

arbitrary concrete type ์„ ์ €์žฅํ•˜๋Š” ๊ฒฝ์šฐ any ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. any ๋Š” type erasure ์„ ์ œ๊ณตํ•˜์—ฌ ์„œ๋กœ ๋‹ค๋ฅธ ์ข…๋ฅ˜๋กœ ์ด๋ฃจ์–ด์ง„ collections ์„ ํ‘œ์‹œํ•˜๊ณ  optional ์„ ์‚ฌ์šฉํ•˜์—ฌ underlying type ์˜ ๋ถ€์žฌ๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์„ธ๋ถ€์ ์ธ ๊ตฌํ˜„์‚ฌํ•ญ์„ ์ถ”์ƒํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ some ์„ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ์ž„์˜์˜ ๊ฐ’์„ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค๋ฉด any ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

40

์ด ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด storage flexibility ๊ฐ€ ํ•„์š”ํ•  ๋•Œ type erasure ์™€ semantic limitations ๋งŒ ์ง€๋ถˆํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ์›Œํฌํ”Œ๋กœ์šฐ๋Š” ๋งˆ์น˜ mutation ์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ๋•Œ๊นŒ์ง€ ๊ธฐ๋ณธ์ ์œผ๋กœ let-constants ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

41 42