Is scala pure object-oriented programming language??

Most of the people have question “Is scala pure object oriented programming language??” if yes then what about functions and primitives.
yes scala is pure object oriented language and functions and primitives are also objects in scala.

Functions as an object.

Internally function is nothing its a trait with apply function. Let see a Function1 trait with apply function as follows

trait Function1[A,B]{
def apply(x:A):B

Above trait shown its take one parameter as type A and return value of type B so its make a function of type Int => Int = <function1>

if we make a annoynomous function as below

val f=(x:Int)=>x*x

internally it would be

val f=new Function1[Int,Int]{
def apply(x:Int)=x*x

And when we call the function internally its call apply function of the trait
Like,if we call above function like below


internally it will call apply function of the trait


and when we make a method it is not a function value internally it will be converted into anonymous function of type Function1,Function2 traits and expand as follows

new Function1[Int,Int]{
def apply(x:Int)=f(x)

The conversion of method to anonymous function this expansion expansion is known as eta expansion

Primitives as a Function

Internally all primitives are also classes which have methods for operators which is apply on object of that class type let see example of Boolean type

abstract class Boolean{
def ifThenElse[T](t: =>T,e: =>T):T
def &&(x: =>Boolean):Boolean=ifThenElse(x,false)

Internally true and false are object who overload the behaviour of methods.

object true extends Boolean
def ifThenElse[T](t: =>T,e: =>T)=t

object false extends Boolean
def ifThenElse[T](t: =>T,e: =>T)=e

In scala every primitive is object and operators are function so we can write 3+3 object oriented translation is 3.+(3)
Here,3’s are objects and + is function class specification Int as follows
class Int{
def +(that:Int):Int
def -(that:Int):Int
def /(that:Int:Int
def *(that:Int):Int

About sandeep

I m working as an software consultant in Knoldus Software LLP . I m working on scala, play, spark,hive, hdfs, hadoop and many big data technologies.
This entry was posted in Scala. Bookmark the permalink.

2 Responses to Is scala pure object-oriented programming language??

  1. Paramesh says:

    Wonderful blog Sandeep. Keep writing

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s