Is scala pure object-oriented programming language??

Table of contents
Reading Time: 2 minutes

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

2 thoughts on “Is scala pure object-oriented programming language??2 min read

Comments are closed.