views:

1342

answers:

19

A lot of programming languages and frameworks do/allow/require something that I can't seem to find the name for, even though there probably is one in computer science. What they basically do is bind to a variable/object/class/function by name.

Flex example ("selectAll()"):

<mx:Button click="selectAll()" label="Select All"/>

Mate example ("price"):

<Injectors target="{QuotePanel}">
  <PropertyInjector targetKey="price" source="{QuoteManager}" sourceKey="currentPrice" />
</Injectors>

Java example ("Foo"):

Class.forName("Foo")

There are many other examples. You get the idea. What troubles me is that there is virtually no way to verify this at compile-time, and not much the IDE can do to help in terms of code completion, navigation, and refactoring. But that's besides the point.

My question is, what is this called? I don't think it's one of these: dynamic binding, name binding, reflection

Update: No, this is not a quiz, sorry if it sounds like one. It's simply a matter of "name that song" for programming.

Update: Answers that helped:

  • From Tim Lesher: It's called "late binding", "dynamic binding", or "runtime binding". The fact that it binds by a string is just an implementation detail...
  • From Konrad Rudolph: ...it's simply input for an interpreter.

Update: As people have correctly pointed out, some of the examples are late binding, some are reflection, some are runtime-evaluation (interpretation), etc. However, I conclude there probably is no name that describes them all. It's just a bunch of examples that do have something in common, but not enough to give it a name. I liked the "everything is a string" answer, but even though it's funny, it doesn't fully do it justice either.

+3  A: 

What makes you think that Class.forName isn't reflection?

Konrad Rudolph
Yes, the Java example IS reflection, and the other examples may be implemented USING reflection under the hood, but the word I'm looking for is NOT reflection.
thvo
No, I really don't know the answer. Apologize if it seems like a quiz.
thvo
+2  A: 

Late binding?

jm
+7  A: 

Late binding

Jon B
+4  A: 

Reflection

Aaron Maenpaa
A: 

In the .NET world we call this databinding, and it has handled using reflection.

It also reminds me strongly of dependency injection.

FlySwat
Databinding is setting properties on a component so that the component serializes its values directly to a database rather than having its values set and read through properties by outside code. Though the question includes the string "Select", it does not appear to be accessing a database.
Jeffrey L Whitledge
Databinding has nothing at all to do with a database. Yes, you can BIND to a database, but you can also bind to an in memory object, and when binding to a database, what do you think its really binding too?
FlySwat
A: 

Smells like a function pointer to me.

Alan
A: 

What are the semantics associated with that syntax?(What happens when that code is executed)

I'm not a Flex/Mate/Java expert.

Paul Nathan
A: 

I agree with Konrad, how do you know what it isn't? It looks like late binding, but you say "no, try again"? Are you asking a question, or giving a quiz?

kdevine
No, I really don't know the answer. Apologize if it seems like a quiz.
thvo
A: 

The Java example you gave is called Dynamic Class Loading. I'm not sure if the other examples are the same thing. But this is useful in reflection. Maybe you are looking for the design pattern called Inversion of control.

Vincent Ramdhanie
+1  A: 

"introspection" ?

Draemon
+3  A: 

The flex thing can be termed as late binding, if it works like normal html. Until user clicks the button the runtime does not bother to find if the function exists or not. The second thing is dependency injection, which involves function pointers (by way of Interfaces) and reflection. The java one is definitely reflection.

I think may be you were not able to word your question properly or had chosen bad examples to illustrate your thought.

TheVillageIdiot
You're right about phrasing the question and finding examples. Maybe there is no word for this.
thvo
the {flex/html} runtime doesn't even cares about trying to find the selectAll function. the parameter is expanded to onclick=eval("function(){selectAll();}") IOW, its late compiling
Javier
+1  A: 

By the way, I surmise that the Flex code you showed us uses simply ActionScript invocation, in which case the click attribute would simply be eval'd by the interpreter of the Flex document. Thus, there's no special magic behind this kind of code, it's simply input for an interpreter.

Konrad Rudolph
Good point about this being input for an interpreter.
thvo
It's actually not interpreted/eval'ed, but rewritten at compile time.
Theo
+13  A: 

It's called "late binding", "dynamic binding", or "runtime binding". The fact that it binds by a string is just an implementation detail, although it does imply that the string-to-symbol mapping exists at runtime (which some languages, like c++, don't provide).

"Introspection" or "reflection", on the other hand, refer to the ability to find out what interfaces, methods, or attributes an object implements at runtime.

It's true that dynamically-bound symbols can't be verified before execution; that's what makes them different from statically-bound symbols.

Tim Lesher
A: 

The first example is an example of how a namespaced xml can assume meanings on compiling time, The second is both a databinding/dependancy injection The third example is Reflection, if I had to tag all this 3 examples with a name I think I will go for "Syntax"

kentaromiura
+1  A: 

I think the Flex example isn't quite the same as the one in Java (don't know the other stuff). The Java example is clearly something I would call late binding, because the class-loader resolves the classname at runtime, the latest possible time to do that.

The Flex MXML is mainly another syntax, which eventually gets compiled to something you could have also written in ActionScript. As far as I can tell, the mx:Button and the selectAll() function are resolved at compile time. At least mxmlc gives errors if you use a undefined symbol there.

Simon Lehmann
+1  A: 

There is a scenario where the compiler can help this... Code Generation.

David B
+1  A: 

If the type of variable is not known until runtime, then it's late binding. If the variable type is known at compile time, then it's early binding.

Intellisense, code completion and all the other IDE features you talk about are only available on early bound variables.

Ricardo Villamil
+1  A: 

The second and third examples are examples of reflection or late binding, but the first example isn't.

<mx:Button click="selectAll()" label="Select All"/>

Is rewritten as ActionScript before compilation, with the selectAll() part being tucked inside an event handler function. Something like this (how it is done exactly can be checked by adding -keep-generated-actionscript to the compiler flags):

_button1 = new Button();
_button1.label = "Select All";
_button1.addEventListener("click", function( event : Event ) : void {
    selectAll();
});

The compiler determines if the attributes are events, styles or properties, but since this is done at compile time it is not reflection. Reflection, by definition, is done at runtime.

I think it could be argued that the second and third examples are reflection, but also that they are examples of late binding. None of them actually determines the capabilities of the objects they work on, so in that way they are not reflection. However, I would say that the term "reflection" is very often used in a broad sense to mean anything that calls methods whose names are determined at runtime, or creates objects from classes named only at runtime. If you want to be precise "late binding" is probably the most correct answer, but I think "reflection" is good enough.

Theo