Saturday, June 19, 2010

Reia: Everything Is An Object



I recently added support for immutable objects to Reia.  Immutable objects work in a similar manner to objects in languages like Ruby, except once created they cannot be changed.  You can set instance variables inside of the "initialize" method (the constructor), but once they've been set, they cannot be altered.  If you want to make any changes, you'll have to create a new object.

Now I've gone one step further: all of Reia's core types are now immutable objects.  This means they are defined in Reia using the same syntax as user-defined immutable objects.  And since Reia looks a lot like Ruby, that means their implementation should be easy to understand for anyone who is familiar with Ruby.  Neat, huh?

When I originally started working on Reia, I drank Erlang-creator Joe Armstrong's kool-aid about object oriented programming.  I wanted to map OOP directly on to the Erlang asynchronous messaging model, and proceeded along that path.  When you sent a message to an object, I wanted that message to be literal, not some hand-wavey concept which was implemented as little more than function calls.

However, this meant concurrency came into play whenever you wanted to encapsulate some particular piece of state into an object.  And if the state of that object never changed, not only was this needlessly complex, it was a total waste!  Furthermore, the core types behaved as if they were "objects" when really they weren't... they pretended to work in a similar manner, but they were special blessed immutable objects.  People asked me if they could implement their own immutable objects, and sadly my answer was no.

Encapsulating immutable states has always been a pain point for Erlang.  The canonical approach, Erlang records, are a goofy and oft reviled preprocessor construct with an unwieldy syntax.  Later Erlang added an "unsupported" feature called paramaterized modules, which feel like half-assed immutable objects.  There are very few fans of either of these features.

The typical OOP thinking is that objects provide a great tool for encapsulating state.  So why do Erlang programmers have to use things like records or parameterized modules instead of objects?  Let's look at Joe Armstrong's reasoning:
Consider "time". In an OO language "time" has to be an object. But in a non OO language a "time" is a instance of a data type. For example, in Erlang there are lots of different varieties of time, these can be clearly and unambiguously specified using type declarations
Okay, great!  So if we try to get the current time in Erlang, what does it give us?

Eshell V5.7.3  (abort with ^G)
1> erlang:now().
{1276,989504,651041}

Aiee!  What the crap is that?  In order to even begin to figure it out, we have to consult the "type declaration":

now() -> {MegaSecs, Secs, MicroSecs}

Okay, this is beginning to make more sense, after consulting the documentation.  What we've received is a tuple, which splits the current time up into megaseconds, seconds, and microseconds since January 1st, 1970.  Microseconds are split off so we don't lose precision by trying to store the value as a float, which makes sense.  However, megaseconds and seconds were split up because at the time the erlang:now()function was written, Erlang did not have support for bignums.  In other words, the type declaration is tainted with legacy.

So what if we have erlang:now() output, how do we, say, convert that into a human meaningful representation of the time, instead of the number of seconds since January 1st, 1970?  Can you guess?  Probably not...

1> calendar:now_to_local_time(erlang:now()).
{{2010,6,19},{17,34,40}}

Of course, the calendar module!  I'm sure that's exactly where you were thinking of looking, right?  No, not really.  The response is decently comprehensible, if you know what time it is.  However, this doesn't seem like a particularly good solution to me.  I guess Joe Armstrong likes his functions and type declarations.  I don't.  So how does Reia do it?

>> Time()
=> #<Time 2010/6/19 17:36:36>

Look at that.  Time is an object!  An immutable object in this case.  Thanks to the fact that there are functions bound to the time object's identity, it automatically knows how to display itself in a human-meaningful manner.  Because the identity of this particular piece of state is coupled to functions which automatically know how to act on it, we don't have to do a lot of digging to figure out how to make it human meaningful.  It just happens automatically.

In the end, I'm a fan of objects and Joe Armstrong is a fan of traditional functional programming principles.  They're both solutions to the same problem, but my argument is Erlang doesn't have a good solution to the problem of user-defined types and coupling the identity of states to corresponding functions designed to act on them.  In the case of the latter, Joe Armstrong thinks it's a bad thing whereas I consider it a basic, essential feature of a programming language.  As for the former, Erlang has given us records and parameterized modules, neither of which are a good solution.

I recently learned (courtesy the JRuby developers) that when Matz created Ruby, he took the core functions of Perl and mapped them all onto objects.  Every single core function in Perl has a corresponding Ruby method which belongs to a particular class.  I am now doing the same thing with Erlang, mapping all of the core functions Erlang provides onto Reia objects.

If Ruby is object-oriented Perl, then Reia is object-oriented Erlang.

20 comments:

luismarianoguerra said...

really nice!

how do you implement objects internally?

Matt Williamson said...

Parameterized modules + record, I'm sure.

johnbender said...

"these can be clearly and unambiguously specified using type declarations"

I think his reasoning is sound only if the type system is much more expressive (ie not just tuples & sugar).

In ghci:

Prelude Data.Time.Clock> getCurrentTime
2010-06-20 21:56:36.344085 UTC

With the type taking the shape:

data UTCTime = UTCTime {
utctDay :: Day
utctDayTime :: DiffTime
}

Even with haskell's less than desirable records it doesn't seem so off putting. In reality if you could provide the Erlang equivalent of a Show instance for your sugared up tuples it could look identical to your example.

Erlang suffers in this area, sure, but the way you've written it makes it sound as though OOP has a better solution for data representation in all cases.

heri16 said...

Really thanks so much for all the progress you have made so far! Keep on keeping on! By the way what happened to Ryan? Is it dead?

Tony said...

Internally Reia's objects are implemented as records and dicts. Parameterized modules aren't presently used as part of the implementation.

Karmen Blake said...

Great post! Just found your blog and I will be catching up on other posts as I get time. I'll be looking into Reia as well.

shina said...

Surveillancekart | CCTV System
cctv installation services
best home security camera system
cctv camera for home
Pestveda | Pest Control
Termite control

mariablack said...

Pc Optimization
Windows Installation
Data Recovery

shabnam praveen said...

Call girls in Kolkata
Call girls in Chandigarh
Call girls in Chandigarh
Call girls in Gurgaon
Call girls in Chandigarh
Call girls in Chandigarh

shabnam praveen said...

Call girls in Lucknow
Call girls in Guwahati
Call girls in Mumbai
Call girls in Jaipur
Call girls in Jaipur
Call girls in Jaipur
Call girls in Bangalore

Dida ELhaik said...


ارقام الاثاث المستعمل جدة
حقين المستعمل بجدة
ارقام الاثاث المستعمل
شراء اثاث مستعمل
شراء الاثاث المستعمل
شراء اثاث
حقين الاثاث المستعمل
حقين المستعمل بجدة
شراء اثاث مستعمل جدة
عزيزي العميل اينما كنت سنعمل على توفير افضل مجموعة من افضل انواع الاثاث المستعمل والذي نوفره لك على اعلى مستوى من التميز والرقي كانه جديد تماما وذلك لكافة انواع الاثاث الكلاسيك والمودرن فقط اطلب ما تريد ونحن نهمل على عرض العديد من المنتجات والانواع المختلفة للاثاث امامك والتي يمكنك الاختيار من بينها وتتوفر لدينا خدمات شحن مميزة في جميع انحاء العالم وطرق الشحن لدينا تحافظ على الاثاث من اى مشكلات سواء كسر او اتربة او غيره نضمن لك وصول الاثاث كما رايته تماما بدون اى مشكلات وباسعار مميزة ولدينا فريق عمل قادر على التواصل معكم في اى وقت لتلبية جميع رغباتكم ومتطلباتكم المختلفة

Juliana Kho said...

s12888
login bluebet66
daftar cbet online
daftar 918 kiss malaysia
osg777 apk

joker123

keanna said...

We are building a group of Amazon Web Services mentors and members for their future assistance and help with subject. Our training will be centered around aiding placements too. We have a different HR group experts who will deal with all your meeting needs. Our Amazon Web Services Training Course Fees is moderate contrasted with others. We are the main Amazon Web Services training institute who can share video surveys of every one of our understudies. We referenced the course timings and begin date also underneath.

For More Info:- AWS Training Course in Gurgaon

dadyar said...

Hey Good news ! an interestingoffering Just click on the link below to qualify
mrdadyar

IT said...

Security testing is must. Everybody wants security at every level of his working. as explained above failures are happened but if we test time to time then it will improve. So it depend on us how much we are serious about this.
Power System Dynamic State Estimation Motivations,Definitions, Methodologies and Future Work Project For CSE
Real Time Lateral Movement Detection based on Evidence Reasoning Network for Edge Computing Environment Project For CSE
Reliable Detection of Interest Flooding Attack in Real Deployment of Named Data Networking Project For CSE
A Lightweight and Efficient Secure Hybrid RSA (SHRSA) Messaging Scheme With Four Layered Authentication Stack Project For CSE
A Remote Sensing Image Encryption Scheme Using DNA Bases Probability andTwo Dimensional Logistic Map Project For CSE

steve said...



غسيل خزانات بمكة شركة غسيل خزانات بمكة
غسيل خزانات بجدة شركة غسيل خزانات بجدة
غسيل خزانات بالدمام شركة غسيل خزانات بالدمام

yasmina said...

شركة تنظيف منازل بالجبيل

شركة تنظيف منازل بالدمام

شركة تنظيف منازل بالقطيف

شركة تنظيف مجالس بالجبيل


Why should you hire a cleaning company for home?
House cleaning is part of home maintenance. It is a fact of life that must be made from dishes, washing clothes, removing dirt from the ground and so on.
No one really enjoys the hostage work, but it can't go on unattended for a long time.
While everyone can agree that cleaning the house should be done, but no one really enjoys it, people are still hesitant to think about a house cleaning company.
This happens for a number of reasons; people have doubts about strangers in their homes, they don't want people to deal with their
personal belongings, they don't feel they can justify expenses and are worried about the damage they do.

geekymr said...

nordvpn mod apk is best way to secure your online privacy. pubg mobile mod apk gives you unlimited UC

geekymr said...

isaimini tamil movies download is a free website to download tamil movies.
list of top10 webiste to download tamil dubbed movies free download

angelisaka97 said...

Permainan poker pastinya banyak di kalangan remaja hingga dewasa yang sangat menggemari permainan poker, apalagi dalam 1 id game ada banyak permainan kartunya silahkan kunjungi situs kami untuk merasakan kenyamanan dalam bermain.

server idnplay


daftar akun idn poker

idn poker deposit pulsa

agen judi poker online

agen idnplay

agen idn poker play

deposit poker menggunakan telkomsel

deposit poker menggunakan xl

daftar poker indonesia


download idn poker

download idn poker apk

apk idn poker

aplikasi idn poker

aplikasi idnplay

idn poker apk versi terbaru

idn poker versi 1.1.14.0

cara download idn poker

cara install idn poker

install idn poker

cara install idnplay

cara idnplay android

cara install idnplay android

cara login aplikasi poker


agen idn poker

agen idn poker deposit pulsa

agen poker idn yang bisa deposit pakai pulsa

deposit poker idn pakai pulsa

deposit poker idn pakai pulsa telkomsel

deposit poker idn pakai pulsa xl

cara deposit idn poker pakai pulsa

deposit idnplay

tambah kredit saldo idn poker

deposit idnplay pakai pulsa

deposit poker uang asli

tambah saldo idnplay