7

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.

Theo
  • 131,503
  • 21
  • 160
  • 205
thvo
  • 1,532
  • 2
  • 15
  • 29
  • Maybe you can explain why you don't like the terms dynamic or late binding. That's what I would have called it. – Michael Burr Oct 13 '08 at 17:41
  • Dynamic/late binding seems related to polymorphism, specifically deciding which method to call. You're right, it's part of it but it doesn't seem to hit the nail on the head since what I'm looking for is also used for setting properties, not just calling methods/functions. – thvo Oct 13 '08 at 17:52
  • The terms "dynamic binding" and "late binding" were in common use since before the early 80's when I was a young college lad. They may be used by the OOP crowd to describe polymorphism, but they certainly predate it for the use you're talking about. – Michael Burr Oct 13 '08 at 18:34
  • 1
    I wonder if it gets me any votes if I post another answer with "late binding?"... – Tomalak Oct 13 '08 at 18:39

17 Answers17

15

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
  • 6,341
  • 2
  • 28
  • 42
7

Late binding

Jon B
  • 51,025
  • 31
  • 133
  • 161
3

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

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 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 Oct 13 '08 at 17:38
  • No, I really don't know the answer. Apologize if it seems like a quiz. – thvo Oct 13 '08 at 17:43
3

Reflection

Aaron Maenpaa
  • 119,832
  • 11
  • 95
  • 108
3

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
  • 40,053
  • 20
  • 133
  • 188
  • You're right about phrasing the question and finding examples. Maybe there is no word for this. – thvo Oct 13 '08 at 17:54
  • 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 Oct 13 '08 at 18:12
2

Late binding?

jm.
  • 23,422
  • 22
  • 79
  • 93
2

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
  • 131,503
  • 21
  • 160
  • 205
1

"introspection" ?

Draemon
  • 33,955
  • 16
  • 77
  • 104
1

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
  • 530,221
  • 131
  • 937
  • 1,214
1

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
  • 10,737
  • 4
  • 41
  • 53
1

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

Amy B
  • 108,202
  • 21
  • 135
  • 185
1

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
  • 5,031
  • 2
  • 30
  • 26
0

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

It also reminds me strongly of dependency injection.

FlySwat
  • 172,459
  • 74
  • 246
  • 311
  • 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 Oct 13 '08 at 20:45
  • 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 Oct 13 '08 at 21:21
0

Smells like a function pointer to me.

Alan
  • 3,815
  • 1
  • 26
  • 35
0

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
  • 102,349
  • 23
  • 137
  • 192
0

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
  • 6,459
  • 2
  • 21
  • 15