Variance

scala variances

Scala Variances: Covariance, Contravariance, and Invariance

Reading Time: 4 minutes 1. Variance Variance is the interconnection of subtyping relationship between complex types and their component types. Variance is all about sub-typing. It tells us if a type constructor is a subtype of another type constructor. Variance defines inheritance relationships of parameterized types(types that have parameters within them). Sub-Typing Every programming language supports the concept of types. Types give information about how to handle values at Continue Reading

Type Parameterization

Reading Time: 4 minutes Introduction Hello everyone, in this blog we discussed Type Parameterization in Scala. It generally covers generic, types bounds and variance in Scala. What is type Any class, trait or object is a type Anything defined by type keyword is a type. For example, type A = String Why we use type parameterization? Type parameterization allows you to write generic classes and traits. For example, sets Continue Reading

Generic Traits and Classes With Type Parameters And Variance

Reading Time: 4 minutes Variance is the correlation of subtyping relationships of complex types and the subtyping relationships of their component types. Scala supports variance annotations of the type parameters of generic traits and classes.

Scala: Generic classes and Variance

Reading Time: 4 minutes Generic classes are classes which take a type as a parameter. This means, one class can be used with different types without actually writing down it multiple times. They are particularly useful for collection classes. Defining a generic class: Generic classes take a type as a parameter within square brackets [ ]. One convention is to use the letter A as type parameter identifier, though Continue Reading

Back2Basics: Catalogue #1

Reading Time: 2 minutes Knoldus Inc is proud to announce the launch of program “Back2Basics”. Back2Basics is our effort to add value to the Scala ecosystem by exploring not so explored concepts in Scala. The idea behind the inception of the program is to develop an understanding of Scala concepts that are hard to understand and not frequently used by developers. In this program, we have two parallel tracks Continue Reading

Back2Basics: Demystifying Variance

Reading Time: 5 minutes In this blog, we will explore types of variances in Scala. In-variance Co-variance Contra-variance First, let’s talk about Types in Scala. scala> import java.util.ArrayList scala> var ref1: Any = “Hi” ref1: Any = Hi scala> var ref2: String = “Hi” ref2: String = Hi scala> ref1 = ref2 ref1: Any = Hi In the above example, ref1 is of type Any and ref2 is of Continue Reading

Back2Basics: Scala Type System in Depth

Reading Time: 5 minutes In this blog, I will put emphasis on the power and awesomeness of the Scala Type System. Also, I will try to reiterate that it is not difficult or complicated as perceived.  In layman terms, the Scala Type system helps us keep the code tidy and type safe.  So in this blog, I shall take you through the following : Parameterized types In-Variance Co-Variance Contra-Variance