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().

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()).

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.


luismarianoguerra said...

really nice!

how do you implement objects internally?

Matt Williamson said...

Parameterized modules + record, I'm sure.

Unknown 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.

Unknown 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 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

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

steve said...

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

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

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

augustwalker said...

Garmin offers a variety of items for a wide range of competitors including Garmin GPS Watches, smartwatches, and wellness trackers.

Darren Demers said...

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? designer asian clothes uk , asian attire uk , asian style dresses uk , stitched asian suits uk , punjabi kurta pajama uk , ready made lawn suits uk , pakistani bridal dresses online usa , white cotton chikankari anarkali , yellow chikankari suit , chikankari suits online , chikankari kurti with palazzo

How they do said...

Excellent Post...I must thank you for this informative news...This is my first time to go to see it here and I am really pleased to read
it all in one place. Cctv camera dealers in Mumbai move to CCTV camera installation services in Mumbai, CCTV camera price in Mumbai @

Lillian J. Turner said...

Nice Blog. Thanks for sharing with us. Such amazing information.

MyBlogger Club

Guest Posting Site

Best Guest Blogging Site

Guest Blogger

Guest Blogging Site

Mark_Glesson said...

I’m really happy to say it was an interesting post to read. I learned new information from your article, you are doing a great job. Hey there, I’m Jessica. I’m a web developer living in Texas. I am a fan of technology, design, and animals. I’m also interested in martial arts and baking. we are providing is for users to understand the process to Download, Install and Activate Webroot Products. For more information visit our website

Escorts Service Kolkata said...


R1se Hluoluo said...

Selanjutnya ini apabila di antara kamu seluruhnya bermain poker online domino kiu– kiu melewati suatu web agen formal serta terpercaya, informasi yang miliki oleh pemain hendak terpelihara nyaman.

Di karenakan mereka ini mau sekali apabila membernya merasakan sarana yang tidak bisa di cari melewati web poker domino kiu– kiu online abal– abal.

Bermain poker online yang menggunakan duit asli tampaknya hendak lebih seru serta pula terus jadi ketagihan buat memainkannnya apabila kamu kita terus merasakan kemenangan di masing- masing game. Hendak namun tidak banyak orang yang mengenali bila game poker ini wajib dimainkan menggunkan trik istimewa supaya bandar poker online tidak secara terus menerus menghabiskan kantong para player nya.

Martin Luiz said...

Best task authors in the Pay To Write My Assignment For Me UK can tackle task composing since they have experienced childhood in the English climate. The task aides have examined from school to a more elevated level of review in the UK establishments.

Dlink Extender said...

Worried about the Dlinkap.local Not Connecting issue taking away your peace of mind? Tackle issues effectively using the troubleshooting guides present on our website. said...

I began my journey in this business when I body massage near me
was only 25 years old. Now, after 10 years my spa is famous in hyderabad.If you're thinking of booking an time with us, don't hesitate to reach me on 0000000000 as our services cannot get more superior than this

suonerie gratis said...

Vieni sul sito Web suonerie per android per trovare le migliori suonerie di canzoni oggi.

rennasweety said...

Are you in (Hyderabad) the capital and largest city in Hyderabad? Are you searching for a place to relax after the hustle and bustle of the day? Then visit IZSPA. Our massage experts are on hand to help you unwind and loosen up. You are welcome to IZSPA the home of Pleasure and Relaxation. Explore our website and body to body spa near me find the many pleasures that await you.

Bryan Hertz said...

It was my course subject, but I didn't have enough knowledge to accomplish this, so I pay someone to do my online class. This means that anything can be considered an object, regardless of size or form. This includes things like planets, stars, and even galaxies.

gostopsite 고스톱사이트 said...

Thanks for sharing this amazing article.

sportstotomen 스포츠토토맨 said...

I no uncertainty esteeming each and every bit of it.

19guide03 19가이드03 said...

Marvelous work! Every one of you complete an unfathomable blog,

casinosite 카지노사이트존 said...

Thank you for some other informative blog.

sportstotozone 스포츠토토존 said...

It's hard to find good quality writing like yours these days.

shanjanaarora said...

Couple massages are dope because they're nude massage in chennai a quality time date that provides a truly great way to reconnect with your partner said...

Discuss current and emerging research efforts to further understand the mechanisms and effects of medical massage.body to body massage centre
Speculate on potential breakthroughs and innovations in the field.

lishasingh said...

An abdominal massage also helps to get female to male spa near me 24 hours a person psychologically prepared. There are two types of ways a person can be psychologically prepared and this can be dependent on the type of sport or activity they are going to be carrying out.

nancysweety said...

A foot massage may help improve your mood, female to male body massage in pune stress, energy level and sleep.