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 answerssomeone asked me: why scala provides object
keyword?
i reply object
can:
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
Post a Comment