scala – When can you use traits

When can you use traits?

There is no specific rule when you can use traits, but there is a guideline which you can consider.

a)      If the behaviour will not be reused, then make it a concrete class. Anyhow it is not a reusable behaviour.

b)      In order to inherit from it in Java code, an abstract class can be used.

c)       If efficiency is a priority then lean towards using a class

d)      Make it a trait if it might be reused in multiple and unrelated classes. In different parts of the class hierarchy only traits can be mixed into different parts.

e)      You can use abstract class, if you want to distribute it in compiled form and expects outside groups to write classes inheriting from it.

scala – function call – recursion tail

What is recursion tail in scala?

‘Recursion’ is a function that calls itself. A function that calls itself, for example, a function ‘A’ calls function ‘B’, which calls the function ‘C’.  It is a technique used frequently in functional programming.

 

In order for a tail recursive, the call back to the function must be the last function to be performed.

scala – better language

 In what ways Scala is better than other programming language?

a)      The arrays uses regular generics, while in other language, generics are bolted on as an afterthought and are completely separate but have overlapping behaviours with arrays.

b)      Scala has immutable “val” as a first class language feature. The “val” of scala is similar to Java final variables.  Contents may mutate but top  reference is immutable.

c)       Scala lets ‘if blocks’, ‘for-yield loops’, and ‘code’ in braces to return a value. It is more preferable, and eliminates the need for a separate ternary operator.

d)      Singleton has singleton objects rather than C++/Java/ C# classic static.  It is a cleaner solution

e)       Persistent immutable collections are the default and built into the standard library.

f)       It has native tuples and a concise code

g)      It has no boiler plate code