How to answer "why scala provides `object` keyword"? -



How to answer "why scala provides `object` keyword"? -

this question has reply here:

why singleton objects more object-oriented? 7 answers

someone asked me: why scala provides object keyword?

i reply object can:

create single instance on language level create new instance of other types factories combine different traits

but said "these object can do, not why".

what should reply question?

(i read articles object, can answer: because scala want back upwards module on language level? )

object there avoid static members. scala designed pure object oriented language, , static isn't object oriented. static makes code harder understand mix code doesn't belong conceptually. in general, separating static , instance members idea.

also object reduces boilerplate implementing singleton pattern , makes useless types less present.

just compare:

class someglobalservice { private someglobalservice instance = null private someglobalservice() { <constructor code> } public static someglobalservice getinstance() { if (instance == null) instance = new someglobalservice homecoming instance } <members> } someglobalservice.getinstance().<somemember>

to

object someglobalservice { <constructor code> <members> } someglobalservice.<somemember>

in first illustration bundle polluted useless class someglobalservice defenetely never utilize directly. in scala type less visible someglobalservice.type.

scala object module

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -