{"id":6197,"date":"2021-02-18T05:57:01","date_gmt":"2021-02-18T05:57:01","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=6197"},"modified":"2024-04-12T09:37:13","modified_gmt":"2024-04-12T09:37:13","slug":"variance-in-scala-variances-tour-of-scala","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/","title":{"rendered":"Scala &#8211; Variance"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Variances | Tour of Scala&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Variances | Tour of Scala<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Variance<\/strong><\/p>\r\n\r\n\r\n\r\n<p>Variance is the correlation between the subtyping relationships of complex types and the subtyping relationships of their component types.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>In Scala, variances are a key concept use to define the relationship between types in the context of subtyping and generics. Variances specify how subtyping of parameterized types (generics) relates to subtyping of their type parameters.<\/p>\r\n<p>Scala supports three variance annotations: <code>+<\/code> (covariant), <code>-<\/code> (contravariant), and no annotation (invariant).<\/p>\r\n<ol>\r\n<li>\r\n<p><strong>Covariance (+)<\/strong>: Indicates that a parameterized type is covariant with respect to its type parameter. This means if <code>A<\/code> is a subtype of <code>B<\/code>, then <code>F[A]<\/code> is a subtype of <code>F[B]<\/code>, where <code>F<\/code> is a covariant type constructor.<\/p>\r\n<\/li>\r\n<li>\r\n<p><strong>Contravariance (-)<\/strong>: Indicates that a parameterized type is contravariant with respect to its type parameter. This means if <code>A<\/code> is a subtype of <code>B<\/code>, then <code>F[B]<\/code> is a subtype of <code>F[A]<\/code>, where <code>F<\/code> is a contravariant type constructor.<\/p>\r\n<\/li>\r\n<li>\r\n<p><strong>Invariant (no annotation)<\/strong>: Indicates no variance relationship between a parameterized type and its type parameter. This means <code>F[A]<\/code> and <code>F[B]<\/code> are unrelated even if <code>A<\/code> is a subtype of <code>B<\/code>.<\/p>\r\n<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<p><strong>Covariance<\/strong><\/p>\r\n\r\n\r\n\r\n<p>A type parameter T of a generic class can be <a href=\"https:\/\/prwatech.in\/blog\/#\">made<\/a> <a href=\"https:\/\/www.scala-lang.org\/\">covariant<\/a> by using the annotation +T.<\/p>\r\n\r\n\r\n\r\n<p>Here is the program for your reference:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-506.png\" alt=\"This image has an empty alt attribute; its file name is Screenshot-506.png\" \/><\/figure>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<p><strong>Contravariance<\/strong><\/p>\r\n\r\n\r\n\r\n<p>A type parameter A of a generic class can contravariant by using the annotation -A.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-6200\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-510.png\" alt=\"\" width=\"678\" height=\"89\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-510.png 619w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-510-300x40.png 300w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>Invariance<\/strong><\/p>\r\n\r\n\r\n\r\n<p>Generic classes in Scala are invariant by default.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"614\" height=\"115\" class=\"wp-image-6201\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-512.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-512.png 614w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-512-300x56.png 300w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Variances | Tour of Scala &nbsp; Variance Variance is the correlation between the subtyping relationships of complex types and the subtyping relationships of their component types. &nbsp; In Scala, variances are a key concept use to define the relationship between types in the context of subtyping and generics. Variances specify how subtyping of parameterized types [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1990,1991,1989],"class_list":["post-6197","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-demystified","tag-variance-scala-3-book","tag-variance-positions-in-scala"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Variances | Tour of Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Variances | Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Variances | Tour of Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Variances | Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-18T05:57:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T09:37:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-506.png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/\",\"name\":\"Variances | Tour of Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-506.png\",\"datePublished\":\"2021-02-18T05:57:01+00:00\",\"dateModified\":\"2024-04-12T09:37:13+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Variances | Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-506.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-506.png\",\"width\":617,\"height\":182},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Variance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Variances | Tour of Scala - Prwatech","description":"Master Variances | Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Variances | Tour of Scala - Prwatech","og_description":"Master Variances | Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-02-18T05:57:01+00:00","article_modified_time":"2024-04-12T09:37:13+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-506.png","type":"","width":"","height":""}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/","name":"Variances | Tour of Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-506.png","datePublished":"2021-02-18T05:57:01+00:00","dateModified":"2024-04-12T09:37:13+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Variances | Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-506.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-506.png","width":617,"height":182},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/variance-in-scala-variances-tour-of-scala\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Variance"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/6197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=6197"}],"version-history":[{"count":8,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/6197\/revisions"}],"predecessor-version":[{"id":11370,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/6197\/revisions\/11370"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=6197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=6197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=6197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}