Friday, August 1, 2008

Erlang, Multiple Assignment, and Closures

There's been some discussion on the tubes recently about the value of Erlang's single-assignment semantics, with people weighing in on both sides.

However, no one seems to be asking the key question, namely what happens when you mix closures (which already exist in Erlang) with multiple-assignment variables? That's probably because the answer is a bit messy: you get an object-oriented language.

To see why this is the case, let's go ahead and define a Person class in this (hypothetical) Erlang variant. The Person class has a single Name field and corresponding getter and setter methods:

-record(person,{get_name,set_name}).

new_person(Name) ->
#person{
get_name=fun() ->
Name
end,
set_name=fun(NewName) ->
Name = NewName % Multiple assignment
end
}.
That's all there is to it. The arguments to the function (i.e. Name) act as the fields of the object, and the anonymous functions assigned to the record (i.e. get_name and set_name) act as the methods. This means we can now create Person objects, pass them around, and modify them whenever we want:
Person = new_person("Sean Combs"),
print(Person#person.get_name()),
Person#person.set_name("Puff Daddy"),
print(Person#person.get_name()).
etc...

Why does this matter? Because objects have identity, and identity's a real bitch when it comes to distributed systems. In particular, a lot of questions pop up when you try to send an object over the wire to another process. Do you:

  • Copy the object, in which case any changes made to it on the remote process won't be visible locally

  • Send a reference to the object, such that any time the object is referred to in the remote process, a call is made back to the original process (at great cost to performance)

  • Let the developer indicate on an object-by-object basis which of the previous two options they would prefer

  • Punt, and simply forbid access to mutable variables from within closures.
    This is pretty limiting, to the point where it makes it impossible to even write a simple foreach function. For example, the following code, which adds the numbers [1,2,3] together, wouldn't be allowed (since Sum is mutable and accessed from a closure):
    Sum = 0, 
    foreach(fun(N)-> Sum += N end, [1,2,3]),
    print(Sum)

    Assuming we're OK with this, we still have to deal with the fact that we now have two types of variables in the language: mutable and immutable. Therefore we must either:

    • Prefix immutable variables with "final" to indicate they can be used within a closure

    • Prefix mutable variables with "var" to indicate they cannot be used within a closure

    • Rely on the compiler to infer which variables are mutable and which are used within closures (possibly producing some very cryptic error messages)

    • Take a "snapshot" of all variables in scope when a closure is created, and ignore any subsequent changes to the variable. For example, the following code would print "foo":

      Name = "foo",
      PrintName = fun() -> print(Name) end,
      Name = "bar",
      PrintName().

      But this would print "bar":

      Name = "foo",
      Name = "bar",
      PrintName = fun() -> print(Name) end,
      PrintName().

      Confusing, to say the least.

Personally, I don't find any of these options very appealing. But if you really want multiple-assignment variables in Erlang, you're going to have to pick one of them.

37 comments:

Clover said...

Spotted your write – ups, it’s cool. Very beneficial and interesting there are some ideas I haven’t heard before. Thanks for sharing.

clover
www.n8fan.net

sarah lee said...

I really enjoyed reading your article. I found this as an informative and interesting post, so i think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing this article.


edupdf.org

lee woo said...

Love it! Very interesting topics, I hope the incoming comments and suggestion are equally positive. Thank you for sharing this information that is actually helpful.


ufgop.org
ufgop.org

Anuska JAIN said...

It is a pressurized situation of managing the recipes items and it has to be cleaned and cleaned efficiently. We should also use the unique material and sponge to make it dry. Packers and Movers Pune
Packers and Movers Bangalore
Packers and Movers Delhi
Packers and Movers Gurgaon

Anuska JAIN said...

If we use the soap 100 % 100 % absolutely 100 % totally 100 % totally free plate fresh bar items, it can be cleaned definitely. Packers and Movers Chennai
Packers and Movers Hyderabad
Packers and Movers Mumbai
Packers and Movers Noida

andrea chiu said...

Thanks for sharing your article and for giving us the chance to read it. It is very helpful and encouraging. Visit my site too.

triciajoy.com

www.triciajoy.com

John Cena said...

Thanks for nice information

Packers and Movers Bangalore
Packers and Movers Pune

John Cena said...

Thanks to post this helpful article

Packers and Movers Bangalore
Packers and Movers Pune

Hot Gurgaon Call Girls said...

Escort in connaught place
Escort in dlf phase
Escort in golfcourse
Escort in sushantlok
Escort in sarjapur
Escort in manesar
Escort in neemrana
call Girls in behror

Sandhya Mane said...

Mumbai Escorts
Mumbai Escorts Service
Juhu Escorts Service
Call Girl Mumbai 
Bandra Escorts Service
Escort Girls in Mumbai

Delhi Escorts Service
Pune Escorts Service
Bangalore Escorts Service
Goa Escorts Service
Chennai Escorts Service
Kolkata Escorts Service
Ahmedabad & Surat Escorts Service
Darjeeling Escorts Service
Mumbai Escorts Service

Cindy Dy said...

I am very happy to locate your website. I just wanted to thank you for the time you spent on this great article. I definitely enjoyed reading it and I have you bookmarked to check out new stuff you post.


Kris
www.gofastek.com

Anonymous said...

Thanks for your post. Click to play !

- Mortal Kombat XL
- Atari Breakout
- Dragon Ball Z Games

packers and movers hyderabad said...

There's a lot of interesting info here! I'm very grateful for the post! movers and packers hyderabad , is here to make your life simpler.
Packers And Movers Hyderabad Packers And Movers is recognized as a business manager providing wide-ranging and differentiate service appearance as well as Relocation Shifting, Logistics and Transportation, Facilities managing, strategy & Designing services.

Packers And Movers Jaipur said...

An excellent information provided thanks for all the information i must say great efforts made by you.
thanks a lot for all the information you provided.
please visit our official website for further details-
Packers And Movers Jaipur

jon rai said...


For More details:-
Movers and Packers in Hyderabad
http://www.shiftingexpert.in/packers-and-movers-hyderabad.html
Movers and Packers Pune
http://www.shiftingexpert.in/packers-and-movers-pune.html
Movers and packers gurgaon
http://www.shiftingexpert.in/packers-and-movers-gurgaon.html
movers and packers Bangalore
http://www.shiftingexpert.in/packers-and-movers-bangalore.html
movers and packers Mumbai
http://www.shiftingexpert.in/packers-and-movers-mumbai.html
Movers and Packers delhi
http://www.shiftingexpert.in/packers-and-movers-delhi.html

jon rai said...


[url=http://www.shiftingexpert.in/packers-and-movers-hyderabad.html] packers and movers in hyderabad [/url] @ [url=http://www.shiftingexpert.in/packers-and-movers-hyderabad.html]http://www.shiftingexpert.in/packers-and-movers-hyderabad.html[/url]
[url=http://www.shiftingexpert.in/packers-and-movers-pune.html]Packers and Movers in Pune [/url]@ [url=http://www.shiftingexpert.in/packers-and-movers-pune.html]http://www.shiftingexpert.in/packers-and-movers-pune.html[/url]
[url=http://www.shiftingexpert.in/packers-and-movers-bangalore.html] Packers and Movers Bangalore [/url]@ [url=http://www.shiftingexpert.in/packers-and-movers-bangalore.html]http://www.shiftingexpert.in/packers-and-movers-bangalore.html[/url]
[url=http://www.shiftingexpert.in/packers-and-movers-gurgaon.html]Packers and Movers Gurgaon[/url] @ [url=http://www.shiftingexpert.in/packers-and-movers-gurgaon.html]http://www.shiftingexpert.in/packers-and-movers-gurgaon.html[/url]

jon rai said...

For Free Query Visit:-

Packers and movers in Bangalore @ http://www.shiftingexpert.in/packers-and-movers-bangalore.html
Packers and Movers Hyderabad @ http://www.shiftingexpert.in/packers-and-movers-hyderabad.html
Packers and Movers Pune @ http://www.shiftingexpert.in/packers-and-movers-pune.html
Packers and Movers Mumbai @ http://www.shiftingexpert.in/packers-and-movers-mumbai.html
Packers and Movers Gurgaon @ http://www.shiftingexpert.in/packers-and-movers-gurgaon.html
Packers and Movers Pune @ http://www.shiftingexpert.in/packers-and-movers-pune.html

Jagat Tiwari said...

Packers and Movers in Hyderabad
Packers and Movers in Bangalore

Jagat Tiwari said...

Packers and Movers in Delhi
Packers and Movers in Gurgaon

Jagat Tiwari said...

Packers and Movers in Hyderabad
Packers and Movers in Bangalore

Jagat Tiwari said...

Packers and Movers in Mumbai
Packers and Movers in Pune

Ankansha said...

There's nothing called failure as we call it. I call it minimal effort that can be turned around.
http://goatripsindia.com/

Chaitali Roy said...

Packers and Movers Bangalore
Packers and movers Pune
Packers and Movers Pune
Packers and Movers Bangalore

Relocate Services said...

Packers and Movers in Bangalore
Packers and Movers in Delhi
Packers and Movers in Jaipur
Packers and Movers in Pune

Aahna Desai said...

Bangalore Escorts Services
Call Girls in Bangalore
Escorts in Bangalore
Call Girls in Bangalore

packers movers bangalore said...

Generally I don't read anything but thanks for writing when i read this i felt how graceful as a writer you are.thank you for posting-
Packers And Movers Bangalore

Mahi Sehgal said...




Escorts Service in Hyderabad
Hyderabad Escorts Service
Hyderabad Escorts
Escorts in Hyderabad

payal sharma said...

An excellent information provided thanks for all the information i must say great efforts made by you. thanks a lot for all the information you provided.
Packers And Movers Pune charges

Noor jaipur said...

Are you living in Jaipur or Jodhpur you will be staying here in a hotel, then our Escort Jaipur Noor Lady already with you within the hour. Starting at 5K you can hire a lady.

Packers Movers Gurgaon said...

Hi – It’s good to read such interesting content. I agree with much of what is written here and I’ll be coming back.
Thanks again for posting such great reading material!! To get more relevant information visit here
Packers And Movers Gurgaon
Packers And Movers In Gurgaon charges

Packers Movers Gurgaon said...

We Provide Best Packers And Movers Gurgaon List for Get Free Best Quotes, Compare Charges,
Save Money And Time, Household Shifting Services @ http://packers-and-movers-gurgaon.in/
Packers And Movers Gurgaon charges

Relocate Services said...

This blog is very helpful to choose a moving company...
We are RelocateServices.in means a unit of Best Packers and Movers in Bangalore or any other city of India. If you want a tension free shifting at affordable charges then you are a right place we will help you to reach the best moving company. We will provide free quotations and free survey for your shifting so that you can compare the charges and services and you can choose any one which is suitable for you.
for more info visit-
Movers and Packers Bangalore
Movers and Packers Delhi
Movers and Packers Jaipur
Movers and Packers Pune

raksha jain said...

very good post which I really enjoy reading. It is not every day that I have the possibility to see something like this.
Packers and Movers Mumbai
Packers and Movers Mumbai

Meera Singh said...

Indian Independent ludhiana escort whatsapp real pictures, photos in ludhiana,

cara menggugurkan kandungan said...

This blog is so nice to me. I will continue to come here again and again. Visit my link as well. Good luck
http://www.jualobataborsiherbal.com/ obat aborsi
http://caramenggugurkankandungan.info/ cara menggugurkan kandungan
http://obataborsi59.com/ obat aborsi
http://obataborsi59.com/cara-menggugurkan-kandungan-dengan-cepat-dan-aman/ cara menggugurkan kandungan
http://obattelatdatangbulan.info/ obat telat datang bulan
http://klinikobataborsi.com/ jual obat aborsi
http://jualobatpenggugurkandungan.net/ obat penggugur kandungan
http://tandatandakehamilan.net/ tanda tanda kehamilan
http://tandatandakehamilan.net/cara-cepat-dan-selamat-menggugurkan-kandungan/ cara menggugurkan kandungan

Meera Singh said...

If you are looking for the best nashik escort Service.

Neha Tripathi said...

24/7 Noida Escorts Services open 24hrs.Contact us to hire Noida Escorts Provider.Find 100% Independent Escorts profiles and reviews.