New topic: Pairs/pass by name example posted
<http://forums.realsoftware.com/viewtopic.php?t=25244> Page 1 of 1 [ 1 post ] Previous topic | Next topic Author Message gisborne Post subject: Pairs/pass by name example postedPosted: Fri Nov 14, 2008 6:55 pm Joined: Tue Feb 06, 2007 11:34 pm Posts: 67 Location: Austin, TX I just posted another example from my OOP Book. Funny story: at the last REAL World conference, I was chatting with Mars about language design (as you do). I suggested that being able to pass parameters to methods by name rather than just position was a very nice feature for a language to have. For example, in Ruby, you can do the likes of: Code:some_method :first_arg => 1, :some_other_arg => 5, :some_option => :foo, :some_other_option => 17, :description => "This is much easier to read than just a list of arguments" Mars agreed, and we talked about how to adapt Dictionaries for this purpose. It seemed that it would be nice to be able to pass a ParamDictionary, much like a ParamArray. But then, you'd want a nice concise syntax for an in-line dictionary. Mars did Pairs more or less on the spot. Mars left before ParamDictionary could be added to the language. So we wound up just with Pairs, and a lot of people going "Huh?". Fortunately, pairs are useful for a lot of things, and some examples have been discussed. But I thought about the original intent, and realized you could actually come pretty close just with pairs. So for my book on OOP, I wrote up the idea and wrote a project illustrating how to do call by name in REALbasic. It lets you write a method like this: Code:Sub Test(ParamArray ps() As Pair) Dim d As Dict = DefaultsDict(ps, "x":"Foo", "y":"Bar","z":"Baz") Break End Sub What that does is, in one line, take the parameter pairs, and convert them to a dictionary. And the list of pairs "x":"Foo", "y":"Bar","z":"Baz" supplies default values for the keys x, y, z if they weren't supplied in the args to the method. You can then call the method like so: Code:Module1.Test "x" : "GOOD", "bleb" : "weird" So even if you don't need my OOP book, you might find the project interesting, both as an illustration of how to use pairs, and because it gives you a new way to make your code easier to read and write. _________________ Guyren G Howe Relevant Logic LLC guyren-at-relevantlogic.com ~ http://relevantlogic.com REALbasic, PHP, Ruby/Rails, Python programming PostgreSQL, MySQL database design and consulting Technical writing and training Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 1 post ] -- Over 900 classes with 18000 functions in one REALbasic plug-in. The Monkeybread Software Realbasic Plugin v8.1. <http://www.monkeybreadsoftware.de/realbasic/plugins.shtml> [email protected]
