Swift เป็นภาษา Type-safe อันดับต้นๆ วันนี้ ???
โอ้ โม่น! ภาษา Type-safe หมายถึงอะไร? ?
ภาษา Type-safe รับรองว่า Operation ทำงานกับ Data Type ที่ถูกต้องในจุดนั้น ✓
ถ้าภาษาประกาศ Data Types ต่างๆ (Int, Float, String, Array, Dictionary) และรับรองว่า Variable ที่ประกาศ Type ไหน จะไม่เก็บ Data Type อื่นได้ เรียกว่า Type-safe Language
ใน Type-safe Languages Type Checking ทำเสมอ อาจเกิด Compile Time หรือ Run Time ขึ้นอยู่กับภาษา ✅
Generics ใน Swift คืออะไร??
Generics เป็น Type-safe ช่วยเขียน Functions และ Types ที่ยืดหยุ่น Reusable ด้วย Generics เขียนโค้ดทำงานกับ Data Types ทั้งหมดได้ โดยไม่ลังเล Type-safety ของ Swift
ตัวอย่างง่าย: Array เป็น Ordered Collection เก็บ Data Type เดียวกัน ใน Definition ของ Array มองเห็น Generic Type Element Array จึงเป็น Generic Type ของ Collection
โอเค กดแล้ว Any Type ใน Swift คืออะไร????
Swift รองรับ Any Type ตามชื่อ แทน Instance ของ Type ใดๆ เช่น Struct, Class, Enum, Function Type
Objective-C id และ Swift Any เหมือนกันไหม??
Swift 3 Objective-C id Map ไป Swift Any Type ปรับปรุง Compatibility Swift และ Objective-C
แต่ทำไมและอย่างไร??
Swift 2 Objective-C id Map ไป Swift Any Object ทำงานดีกรณีส่วนใหญ่ แต่บางครั้ง Unexpected Behavior Key Concept ของ Swift คือ Value Types Mapping นี้ไม่ทำความยุติธรรมกับ Concept นี้
Swift ภาษาใหม่สำหรับ iOS Development Objective-C มานานหลายปี โปรเจกต์ส่วนใหญ่พัฒนาด้วย Objective-C ตอน Convert Objective-C Project ไป Swift ต้อง Bridge Swift Type ใดๆ ไป Objective-C Object ใดๆ ได้
แต่ไม่ใช่ปัญหาสำหรับ Swift Classes และ Value Types เช่น Int, String, Float เพราะมี Objective-C Counterparts อยู่แล้ว ปัญหาเกิดกับ Swift Value Types ที่ไม่มี Objective-C Counterparts
แก้โดย Map Objective-C id Type ไป Swift Any Type ✅✅✅
พอ Definition แล้ว ตอนนี้หัวข้อหลัก จากข้างต้น Generics และ Any Type ดูเหมือนกัน แต่จริงๆ แล้วเหมือนกันไหม???
ภาพรวม Any ดูคล้าย Generics ลองหาความต่าง-???
เรารู้ Stack ใน Data Structures ไหม? Stack เป็น Linear Data Structure พื้นฐาน Insert/Delete ที่ปลายเดียว
Implement Stack ใน Swift ก่อนด้วย Generics แล้วค่อย Any Type
Stack Implementation ด้วย Generics:
Stack Implementation นี้ใช้ Generics Struct รับ Generic Type Element Item Implement Stack ด้วย Item นั้น ลอง Operation กับ Generic Stack:
ประกาศ Generic Stack รองรับ Integer Element Push Integer Element เข้า Stack จนตนนี้ทำงานดี
แต่ถ้าอยาก Push Float Element เข้า Generic Stack ข้างบน?

❌❌ โอ้โห! Compilation Error! ❌❌
Stack Implementation ด้วย Any Type:
Stack Implementation นี้ Items Array เก็บ Any Type Element ไม่ระบุ Data Type ที่แน่นอนของ items Array Element ใน Definition ลอง Operation เหมือนกัน:
ไม่มีปัญหา ทุกอย่างทำงานดี ประกาศ Stack Push Integer 2 ตัว เรียก show() Print Array [3, 4]
ลอง Push Float Value เข้าไป ☄️

✅✅ ไม่มี Error! ทำงานได้ปกติ! ✅✅
เกิดอะไรข้างหลัง? ทำไมไม่ Error????
Generics บอก Compiler ว่า:
ผมประกาศ Generic Type และจะให้ Type ที่แน่นอนภายหลัง ขอให้ Apply Type นั้นทุกที่ที่ระบุ
Any Type บอก Compiler ว่า:
别管这个变量,不需要在这里应用任何类型,让我随心所欲地做我想做的事。
Generics ใช้ Define Flexible Functions ได้ แต่ Types ของ Arguments ยังถูก Check โดย Compiler Any Type ใช้หลบพ้น Type System ของ Swift ?
ใน Generic Stack Declaration บอก Compiler Stack รับ Integer Type เท่านั้น ตอน Insert Float Element แปลว่า Break Promise Compiler Throw Compile Time Error คาดหวัง Element เป็น Integer เสมอ
แต่ Any Stack ไม่ได้ Compile Time หรือ Runtime Error เรียก show() Print Stack [3, 4, 5.0] Stack เก็บ Integer และ Float ได้ Any Stack ไม่มี Type Restriction Push Any Type ได้ (แต่มีโอกาส Runtime Exceptions)
สรุป
ใช้ Generics เขียน Flexible Functions, Structs, Classes, Protocols ได้โดยไม่犠牲 Swift Type Safety แต่用 Any Type เราคือ Boss ของตัวเอง ทำอะไรก็ได้เกือบหมด
??? Cheers!!! ขอบคุณที่อ่าน!! ???