FP Flashcards
S is a subtype of Intset.We can also say that type S is upperbounded by type IntSet
It means that S can be instantiated only to types that conform to IntSet.
What is Liskov Substituion Principle
When one can to do with a value of type B
one should also be able to do with a value of type A.
Eg: Everything we can do with a fruit, we can do with a banana. Everything we can do with an animal, we can do with a Cat and so on…
How is Covariance and Contravariance denoted in Scala
What is Covariance ?
Covariance allows a generic type to preserve the subtype relationship. In other words, if type B is a subtype of type A, then Container[B] is a subtype of Container[A].
Why is Covariance useful ?
This is useful in situations where you want to create collections or structures that can hold more specific types but still be treated as holding more general types.
What is Contravariant ?
Why is Contravariant useful ?
This is useful when you’re consuming values but not producing them. For example, let’s say you have a function that can accept any type of Animal, but you want to pass in a more general handler that can accept Dogs or Cats.
what is type parameterization ?
When we say a class, trait, or method is type parameterized, it means that it can accept a type as a parameter (called a type parameter) that gets specified later, when you create an instance of the class or call the method.
In the contexte of Polymorphisme what are bounds ?
Bounds are constraints from subtypes applied to types parameters.
in the context of Polymorphisme what is Variance ?
How type parameters behaves when subtyping occurs.
What a is a generic type ?
A generic type (or type parameterization) is a type that is defined with one or more type parameters, allowing it to work with different types without specifying the exact types in advance. It provides a way to write reusable and flexible code that can operate on various data types while maintaining type safety at compile-time.
What is a generic class ?
A class that is defined with one or more type parameters, allowing it to work with different types without having to specify those types exactly in advance
Eg: A List
What is the difference and the relation between a class and a type
Une classe est une définition ou une description d’un objet. Elle décrit comment un objet est construit et comment il se comporte. Elle est souvent associée à la création d’instances (objets) et contient des méthodes et des champs.
Un type est une catégorie de données qui décrit les valeurs que peut prendre une variable. Il peut être défini par une classe (comme Person), mais il peut également être autre chose (comme un type primitif ou un type abstrait).
Among Arrays and list which one are covariant which one isn’t and why ?
Arrays aren’t covariant because they are mutable while list are not mutable. Since you cannot modify the list, you can’t accidentally add a Cat to a list of Dogs.
What are the typing rules for function ?
if A2> A1 and B>B2 then
A1 => B1 < A2 => B2
how functions behaves in terms of variance regarding their type arguments and their return type
function are contravariant in their argument and covariant in their result type
Can Objects have parameters. If yes or no why ?
They can’t because there’s only one instance of an object