{"id":4393,"date":"2018-11-23T09:50:21","date_gmt":"2018-11-23T09:50:21","guid":{"rendered":"https:\/\/www.capitalnumbers.com\/blog\/?p=4393"},"modified":"2026-05-12T10:23:01","modified_gmt":"2026-05-12T10:23:01","slug":"how-to-create-compile-time-polymorphism-using-swift","status":"publish","type":"post","link":"https:\/\/www.capitalnumbers.com\/blog\/how-to-create-compile-time-polymorphism-using-swift\/","title":{"rendered":"How to Create Compile Time Polymorphism using Swift"},"content":{"rendered":"<p>Polymorphism is one of the base pillars of Object-oriented Programming (OOPs). Almost every developer who starts to code comes to realize this one way or the other. And then there are the varied formats of polymorphism, which may look daunting at some moments, with the technical jargon.<\/p>\n<p>Polymorphism is many (poly) forms (morphs), which basically means one variable, one function, etc. can be in multiple formats. There are many ways to achieve polymorphism.<\/p>\n<p>Today we are here to discuss one of those multiple formats, i.e. Compile Time Polymorphism or better known as Parametric Polymorphism.<\/p>\n<p>Instead of defining multiple entities, we are trying here to make the logical approach in a more generic way; it will be accessed by as many entities as possible. This forms the basis of generic programming, where instead of creating multiple classes to define the logic, we aim to create a generic structure where the logic can be contained.<\/p>\n<p>So how can we learn this? How can we implement this in Swift?<\/p>\n<p>Let\u2019s construct a logic before we move on to code: we want to have a class from where we will be able to identify what kind of language animals speak. For this purpose, we assume two animals &#8211; the Dog and the Cat. Cats say \u201cMeow\u201d, \u201cMeow\u201d, while Dogs say \u201cWoof.\u201d<\/p>\n<p>How can we approach this? We know that there will be a class to accomplish it. Let\u2019s call it Speakers. In Swift, there\u2019s a feature that is often ignored; this is called <a href=\"https:\/\/docs.swift.org\/swift-book\/LanguageGuide\/Generics.html\" target=\"_blank\" rel=\"noopener\">Generics<\/a>.<\/p>\n<h3>As the official doc states :<\/h3>\n<p>\u201cGeneric code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.\u201d<\/p>\n<p>Which means we can employ Swift generics to construct the aforementioned concept here.<\/p>\n<p>First, let\u2019s define two classes as follows.<\/p>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\">class<\/span> Dog {\n}\n\n<span style=\"color: #99cc00;\">class<\/span> Cat {\n}\n<\/pre>\n<p>And there will be a third class called \u2018I\u2019, where the logic to call the corresponding animals will have to be written. (The idea is I call a dog and the response will be printed out)<\/p>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\">class<\/span> I<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> }<\/span>\n<\/pre>\n<p>Now we need to add the speak function to each of the classes.<\/p>\n<p>We will create a protocol here called Response, which will have this speak function. Both Dog and Cat will have to conform to this Response protocol.<\/p>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\">protocol<\/span> Response<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">func<\/span> speak<span style=\"font-weight: 400;\"> () <\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n<\/pre>\n<p>But as we know, a cat will respond to \u201cmeow\u201d \u201cmeow\u201d which clearly is a collection while a dog will respond only to \u201cwoof\u201d, which is a string.<\/p>\n<p>So we will have to make the protocol return a value whose return type may vary based on the class itself.<\/p>\n<h3>Let\u2019s rewrite Response Protocol:<\/h3>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\"><b>protocol<\/b><\/span> <b>Response<\/b><span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;associatedtype <\/span><span style=\"color: #ccffcc;\"><b>Voice<\/b><\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\"><b>func<\/b><\/span> <b>speak<\/b><span style=\"font-weight: 400;\"> () -&gt; <\/span><span style=\"color: #ccffcc;\"><b>Voice<\/b><\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n<\/pre>\n<p>Good. This means any class that conforms to Response Protocol will have a voice to speak.<\/p>\n<p>Let\u2019s add this to our classes Dog and Cat and rewrite it:<\/p>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\">class<\/span> Dog<span style=\"font-weight: 400;\"> : <\/span>Response<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">typealias<\/span> <span style=\"color: #ccffcc;\">Voice<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"color: #ccffcc;\">String<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">func<\/span> speak<span style=\"font-weight: 400;\">() -&gt; <\/span><span style=\"color: #ccffcc;\">String<\/span><span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">return<\/span> <span style=\"font-weight: 400; color: #ff6600;\">\"woof\"<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n<\/pre>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\">class<\/span> Cat<span style=\"font-weight: 400;\"> : <\/span>Response<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">typealias<\/span> <span style=\"color: #ccffcc;\">Voice<\/span><span style=\"font-weight: 400;\"> = [<\/span><span style=\"color: #ccffcc;\">String<\/span><span style=\"font-weight: 400;\">]<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">func<\/span> speak<span style=\"font-weight: 400;\">() -&gt; [<\/span><span style=\"color: #ccffcc;\">String<\/span><span style=\"font-weight: 400;\">] {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">return<\/span><span style=\"font-weight: 400;\"> [<\/span><span style=\"font-weight: 400; color: #ff6600;\">\"meow\"<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400; color: #ff6600;\">\"meow\"<\/span><span style=\"font-weight: 400;\">]<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">} <\/span>\n<\/pre>\n<p>When a class conforms to Response Protocol, it will have to define what its voice value type is. For class Dog, it\u2019s a String, for class Cat it\u2019s an array of String or [Strings].<\/p>\n<p>Now we have to write the Speakers class logic<\/p>\n<p>The idea for call function in \u2018I\u2019 is like this \u2192 \u2018I\u2019 will have a function named call, where the i\/p will be an object of the class that conforms to Response Protocol.<\/p>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\">class<\/span> I<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">class<\/span> func call<span style=\"font-weight: 400;\">&lt;<\/span>T<span style=\"font-weight: 400;\">: <\/span>Response<span style=\"font-weight: 400;\">&gt;(<\/span>_ speaker<span style=\"font-weight: 400;\">: <\/span>T<span style=\"font-weight: 400;\">) -&gt; <\/span>T<span style=\"font-weight: 400;\">.<\/span>Voice<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">return<\/span><span style=\"font-weight: 400;\"> speaker.speak()<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n<\/pre>\n<h3>Let\u2019s break it down:<\/h3>\n<p>The function call() takes in an i\/p which is referred as speaker. This speaker is of T type, where T is referring to Response protocol. The return type is the Voice of the speaker.<\/p>\n<p>Instead of using an actual type for the return value (such as String , Array for this case), we are using a placeholder type (T in this instance), where it represents any class that conforms to Response protocol. Based on what the speaker will be, the return value will vary, i.e. it will be defined in compile time. The call function can have any form of object that conforms to Response and based on that object its response will also be of different types. This is the Parametric Polymorphism.<\/p>\n<h3>Let\u2019s go through the entire logic:<\/h3>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\">protocol<\/span> Response<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;associatedtype <\/span><span style=\"color: #ccffcc;\">Voice<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">func<\/span> speak<span style=\"font-weight: 400;\"> () -&gt; <\/span><span style=\"color: #ccffcc;\">Voice<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"color: #99cc00;\">class<\/span> Dog<span style=\"font-weight: 400;\"> : <\/span>Response<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">typealias<\/span> <span style=\"color: #ccffcc;\">Voice<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"color: #ccffcc;\">String<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">func<\/span> speak<span style=\"font-weight: 400;\">() -&gt; <\/span><span style=\"color: #ccffcc;\">String<\/span><span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">return<\/span> <span style=\"font-weight: 400; color: #ff6600;\">\"woof\"<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"color: #99cc00;\">class<\/span> Cat<span style=\"font-weight: 400;\"> : <\/span>Response<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">typealias<\/span> <span style=\"color: #ccffcc;\">Voice<\/span><span style=\"font-weight: 400;\"> = [<\/span><span style=\"color: #ccffcc;\">String<\/span><span style=\"font-weight: 400;\">]<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">func<\/span> speak<span style=\"font-weight: 400;\">() -&gt; [<\/span><span style=\"color: #ccffcc;\">String<\/span><span style=\"font-weight: 400;\">] {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">return<\/span><span style=\"font-weight: 400;\"> [<\/span><span style=\"font-weight: 400; color: #ff6600;\">\"meow\"<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400; color: #ff6600;\">\"meow\"<\/span><span style=\"font-weight: 400;\">]<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"color: #99cc00;\">class<\/span> I<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">class<\/span> func call<span style=\"font-weight: 400;\">&lt;<\/span>T<span style=\"font-weight: 400;\">: <\/span>Response<span style=\"font-weight: 400;\">&gt;(<\/span>_ speaker<span style=\"font-weight: 400;\">: <\/span>Response<span style=\"font-weight: 400;\">) -&gt; <\/span>T<span style=\"font-weight: 400;\">.<\/span>Voice<span style=\"font-weight: 400;\"> {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #99cc00;\">return<\/span><span style=\"font-weight: 400;\"> speaker.speak()<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n<\/pre>\n<h3>To test, let\u2019s add some more codes to your playground:<\/h3>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\">let<\/span><span style=\"font-weight: 400;\"> response = <\/span><span style=\"color: #ccffcc;\">I<\/span><span style=\"font-weight: 400;\">.call(<\/span><span style=\"color: #ccffcc;\">Dog<\/span><span style=\"font-weight: 400;\">())<\/span>\n\n<span style=\"font-weight: 400; color: #ccffcc;\">print<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400; color: #ff6600;\">\"A dog <\/span><span style=\"font-weight: 400; color: #ccffcc;\">\\(response)<\/span><span style=\"font-weight: 400; color: #ff6600;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\n\n<span style=\"color: #99cc00;\">let<\/span><span style=\"font-weight: 400;\"> catResp = <\/span><span style=\"color: #ccffcc;\">I<\/span><span style=\"font-weight: 400;\">.call(<\/span><span style=\"color: #ccffcc;\">Cat<\/span><span style=\"font-weight: 400;\">())<\/span>\n\n<span style=\"font-weight: 400; color: #ccffcc;\">print<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400; color: #ff6600;\">\"A cat <\/span><span style=\"font-weight: 400; color: #ccffcc;\">\\(catResp)<\/span><span style=\"font-weight: 400; color: #ff6600;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\n<\/pre>\n<h3>Output:<\/h3>\n<pre style=\"background-color: #000; color: #fff; padding: 10px 20px; margin-bottom: 20px;\"><span style=\"color: #99cc00;\">A<\/span><span style=\"font-weight: 400;\"> dog woof<\/span>\n<span style=\"color: #99cc00;\">A<\/span><span style=\"font-weight: 400;\"> cat [<\/span><span style=\"font-weight: 400; color: #ff6600;\">\"meow\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400; color: #ff6600;\">\"meow\"<\/span><span style=\"font-weight: 400;\">]<\/span>\n<\/pre>\n<p>For class Dog, the Voice is a String type, it returns a string, for class Cat, it\u2019s an array.<\/p>\n<p>Capital Numbers is a leading <a href=\"https:\/\/www.capitalnumbers.com\/swift.php\" target=\"_blank\" rel=\"noopener\">Swift app development company<\/a>, ensures to provide end-to-end solutions for the Swift web and mobile iOS app development.<\/p>\n<p>Hope this makes sense. As always, if you have any question or feedback, please feel free to comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism is one of the base pillars of Object-oriented Programming (OOPs). Almost every developer who starts to code comes to realize this one way or the other. And then there are the varied formats of polymorphism, which may look daunting at some moments, with the technical jargon. Polymorphism is many (poly) forms (morphs), which basically &#8230;<\/p>\n","protected":false},"author":20,"featured_media":4394,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false},"categories":[728,744],"tags":[],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/posts\/4393"}],"collection":[{"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/comments?post=4393"}],"version-history":[{"count":26,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/posts\/4393\/revisions"}],"predecessor-version":[{"id":19408,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/posts\/4393\/revisions\/19408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/media\/4394"}],"wp:attachment":[{"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/media?parent=4393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/categories?post=4393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/tags?post=4393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}