Pergunta

Muitas linguagens e estruturas de programação fazem/permitem/exigem algo para o qual eu não consigo encontrar o nome, mesmo que provavelmente exista um em ciência da computação. O que eles basicamente fazem é ligar a uma variável/objeto/classe/função por nome.

Flex exemplo ("selectall ()"):

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

Amigo Exemplo ("Price"):

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

Exemplo java ("foo"):

Class.forName("Foo")

Há muitos outros exemplos. Você entendeu a ideia. O que me incomoda é que praticamente não há como verificar isso em tempo de compilação, e não muito o IDE pode fazer para ajudar em termos de conclusão de código, navegação e refatoração. Mas isso está além do ponto.

Minha pergunta é: como isso se chama? Eu não acho que seja um desses: ligação dinâmica, encadernação de nome, reflexão

Atualizar: Não, isso não é um teste, desculpe se parece um. É simplesmente uma questão de "Nome That Song" para programação.

Atualizar: Respostas que ajudaram:

  • De Tim Lesher: é chamado de "encadernação tardia", "ligação dinâmica" ou "vinculação de tempo de execução". O fato de se ligar por uma string é apenas um detalhe de implementação...
  • De Konrad Rudolph: ...É simplesmente inserida para um intérprete.

Atualizar: Como as pessoas apontaram corretamente, alguns dos exemplos são vinculativos tardios, alguns são reflexões, outras são avaliações de tempo de execução (interpretação), etc. No entanto, concluo que provavelmente não há nome que descreva todos eles. São apenas um monte de exemplos que têm algo em comum, mas não o suficiente para dar um nome. Gostei da resposta "Tudo é uma corda", mas mesmo que seja engraçado, também não faz justiça.

Foi útil?

Solução

É chamado de "encadernação tardia", "ligação dinâmica" ou "vinculação de tempo de execução". O fato de se ligar por uma string é apenas um detalhe de implementação, embora implique que o mapeamento da string-símbolo exista em tempo de execução (que alguns idiomas, como C ++, não fornecem).

"Introspecção" ou "Reflexão", por outro lado, consulte a capacidade de descobrir quais interfaces, métodos ou atributos um objeto implementa em tempo de execução.

É verdade que símbolos dinamicamente ligados não podem ser verificados antes da execução; É isso que os diferencia dos símbolos com limites estaticamente.

Outras dicas

Encadernação tardia

O que te faz pensar isso Class.forName Não é a reflexão?

Reflexão

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.

Late binding?

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.

"introspection" ?

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.

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.

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

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.

Smells like a function pointer to me.

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.

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

It also reminds me strongly of dependency injection.

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"

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top