4 Advantages of immutable over mutable. Give one disadvantage as well and when it would be mitigated.
Advantages:
1. Easier to read, understand and update
2. Can share freely with no risk of state change
3. Prevents race conditions (Thread safe)
4. Safe hash table keys
Disadvantage: A large object structure may need to be fully copied to a new slightly different structure in immutability.
Cases classes provided a built in copy functionality to mitigate this. This is called structural sharing.
What are the special features of case classes?
Explain case classes do not have identity.
Two instances of the same case class with the same values are considered equal. This is not the case with normal classes.
What is copy used for? Syntax for copy using Dog.
Effectively is how we mutate a case class.
case class Dog(name: String, breed: String)
val p = Dog(“Fido”, “Jack Russell”)
val q = p.copy(name = “Rudolph”)
T or F. We usually implement functions outside the case class.
True
c match
case _ if c.SomePredicate => …
What does this code mean?
Matches on anything for which the predicate is true
Syntax for case statement matching on Phone type when we don’t care about parameters or need to use the instance later.
case _: Phone
If we leave out a case, what will happen? What if we make the underlying trait sealed?
Compile fine, run time error if we match something and can’t find a case for it.
Compiler warning that not all cases are handled.
T or F. Case classes should never inherit from a concrete normal class and they should always be concrete.
True
What are the pros and cons of OO approach?
P: 1. More compact
2. More cohesive encapsulated classes.
3. Adding new class easy.
C: 1. Adding new functions hard each class must be changed
What are the pros and cons of the case class approach?
P: 1. Adding new functions easy
2. One new function can handle any class
C: 1. Adding class hard each function must be changed
2. Matching exposes internal representation of matched object making code maintenance harder.
Explain Option class.
Option[A] is an abstract class that represents either Some[A] (a useful value of type A) or None (no value).
T or F. None is a Singleton Object
True, one None object for all uses.
What does s.toIntOption do? What happens if we map this across a list but instead of map use flatMap?
toIntOption will convert a string (s) to a Some[Int] if it can or a None if conversion fails.
Flatten extracts the element from Some and ignores the None(s).
T or F. Map followed by flatten is the same as flatMap.
True.
Define Null Object Pattern.
Null Object of type T has same interface as a real object of type T, but does nothing. Although usually it is an object instead of a class.
Code to implement Null Object Pattern.
object SomeNullObject extends SomeObject:
def someFunction = ()
Ensure all SomeObjects are defaulted to be SomeNullObjects.
What is the limitation of Options and NullPet methods within lists?
Must define new length (and lots of other) function(s) as will include them in calculation.
Define closure. (And the terms in the definition)
A closure is a first-class function with free variables that are bound in the lexical environment.
first-class: Can be treated as variable, parameter etc.
free variable: Not defined within the function
lexical environment: Code enclosing the function where it is created.