Over a million developers have joined DZone.

Naming Of Anonymous, First Class Code Segments

DZone's Guide to

Naming Of Anonymous, First Class Code Segments

Free Resource

One of the things I’m starting to notice when working on Ioke isthat current programming languages doesn’t seem to have enough namesfor first class code segments. There are of course the usual suspects,that most languages have in some or another form. Depending on theevaluation strategy these things can mean slightly different things,but they’re still very few concepts. The different types I can see atthe moment are this

Lambdas: these are anonymous functions that generally tend to beclosures. In some languages lambdas doesn’t have to be lexicalclosures, but can instead have free variables that are dynamicallybound. These distinctions doesn’t seem to have different names. In somelanguages there are only lambdas, such as Scheme. In Ruby lambdas aregenerally called blocks because they are distinct from the otherfunction primitive - named methods. Ruby blocks are of course notreally first class, but they can be reified.

Uhm. Well. That was it. For unnamed, first class code, lambdas seemto be it. Of course there are functions and methods with differentsemantic meaning in different languages. Methods are generally notclosures in the conventional sense - instead they are bound andexecuted in a dynamic scope depending on the receiver of the methodcall, so that free variables in the method will be resolved as comingfrom the receiver of that method call. The difference between functionsand method seem to be that a function doesn’t have an explicitreceiver, while a method has.

This also explains why anonymous methods aren’t that common. Theirmain power lies in resolving names dynamically on the receiver, whichmeans it would be quite hard to create a method at runtime that is alexical closure. How does the implementation know which variables aresupposed to be lexically scoped and which should be scoped to thereceiver? The only default rule would be to check for current lexicalvariables at parse time, and assign all other names to the dynamicscope. And that feels kinda cumbersome, and also like it would be hardto intuitively grasp the rules.

In Ioke I have fn/fnx and method. The result of fn and fnx is aLexicalBlock, and the result of a call to method is a DefaultMethod. InIoke, a method can be anonymous. This means that for those methods youhave to explicitly send in the receiver.

So what’s the problem? Well, the problem is that there are severalother kinds of code that you might come across in Ioke, and it would benice to have some kind of naming strategy for them. Take the simpleexample of doubling numbers in a list. This can be done in twodifferent ways with map: “[1,2,3] map(n, n*2)” and “[1,2,3] map(*2)”.The second example notice that no argument name is given, so assumethat the given argument is a message chain that should be applied toeach element from the list. The first example works like most lexicalcode blocks. It will create a new lexical scope that exists with asurrounding pointer to the place where the call to map happens, andthen set n in that scope, and finally call the code. So that code couldbe more or less equivalent to sending in a lexical block that takes oneargument. It’s more or less the same thing. The second example is notlike that, though. In fact it doesn’t establish any new scope at all.It executes exactly like if you would have executed that code in thecalling context. So what should that kind of code be called?

There are other examples, where you send in a code segment likethat, and that code will be applied in a totally different context. Andthere are other variations in if there is a lookup strategy for doingdynamic lookup on the receiver or not, if the lexical scope AND thereceiver scope should be mixed together, and so on. All if it isexposed in the language, but I know that as soon as I will startwriting a tutorial about this, I will run into the naming issue forreal. And I’m not sure about it. Maybe I can just go on and make upnames that make sense, but on the other hand I’m not sure if thereisn’t already a treasure trove of names already created for differentkinds of anonymous code fragments. Ioke can’t be the first languagewith these kind of features, right?


Published at DZone with permission of Ola Bini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}