¿Cuándo ocurre la instanciación genérica (es decir, qué significa la palabra)?

This question is based on a discussion (now deleted) in response to esta pregunta. It's more about terminology than actual programming semantics.

What exactly is generic "instantiation"? Does it occur at compile time or at run time?

I'll refer to the último borrador of the Ada 2012 standard.

The meaning of the term generic_instantiation is clear; it's a syntactic construct, something like

package Inst is new Generic_Package(This => That);

My question is about the verb "instantiate".

I've always thought of "instantiation" as something that occurs at compile time. It's the expansion of a generic template, and it occurs when the compiler encounters a generic_instantiation in a compilation unit.

But other sources refer to "instantiation" taking place, at least optionally, at run time.

Este artículo de Wikipedia dice:

A instanciar a generic unit, the programmer passes real parameters for each formal. The generic instance then behaves just like any other unit. It is possible to instantiate generic units at run-time, for example inside a loop.

which seems to imply that instantiation can occur either at compile time or at run time. I think this is referring to the elaboración de los generic_instantiation, which does occur at run time -- but then, all elaboration occurs at run time, yes?

I found it surprisingly difficult to get a definitive answer to this from the Ada RM. There is no glossary entry (Annex N) for "instantiation" or "instantiate".

The closest I could find was this statement in 12.2 (Generic Bodies) paragraph 2, under "Dynamic Semantics":

The elaboration of a generic body has no other effect than to establish that the generic unit can from then on be instantiated without failing the Elaboration_Check.

el cual indirectamente implies that instantiation is a run-time event.

Does the verb "instantiate" properly refer to a compile-time event (expanding a generic template)? To a run-time event (elaborating a generic_instantiation)? If the latter, what do we call the former? Is the Ada RM as clear as it needs to be on this point?

preguntado el 09 de marzo de 12 a las 22:03

Seems to me, just based on English, instantiation means creation of an instance, which certainly sounds like run-time. -

@DonRoby: Ah, but what is an "instance"? If an instance is the código that results from expanding the template, then it's compile-time. -

@DonRoby: A loose analogy: C macro expansion definitely takes place at compile time, not run time. Ada generics obviously aren't the same thing, but some of the same principles apply. -

For me the difference between instantiation and elaboration is that a 'normal' unit will have only one possible elaboration, however a generic unit can have more than one instantiation, thereby resulting in more than one elaboration. As such i would postulate that it can happen at either compile time or runtime. (hence the ambiguity present, and the need for this question) -

I Suggest we wait for other Ada people to enlighten us as i suspect we are devolving into a discussion we have already had ... -

2 Respuestas

Según the generic instantiation en el objeto Static Semantics part in the rules 12, 13 and 14, it seems to be done at compile time.

Pero the Gnat documentation, en su elaboration order control part, describe that a Error de programa would be raised under certain circumstances. Such exception is only thrown when running the program.

So the point is, as far as I understood, that at compile time, the compiler checks that arguments support operations and ranges pushed by your generic but that real code is done at runtime.

Respondido el 19 de Septiembre de 12 a las 13:09

"Instantiation" is the act of creating an instance of a type (either an object or a derived type). It also can refer to the creation of an instance of a package or subprogram when speaking of generics. The instance of a type is an object of that type:

type My_Type is
   record
      Range_Begin : Natural := 1;
      Range_End   : Natural := 15_000_000;
   end record;

type My_Type_Access is access all My_Type;

My_Object1 : My_Type_Access := new My_Type;  -- instantiation of My_Type 
                                             -- to create My_Object1

My_Array_Objects : array (Positive range 1 .. 250_000) of My_Type_Access :=
   (others => new My_Type);  -- creates an array of objects of My_Type

"Elaboration" is the process of preparing a construct such as a package or task for loading and execution. Haven't studied this recently, however.

respondido 12 mar '12, 19:03

I've seen the word "instantiation" used to refer to creating an instance (object) of a type, but that's not what I was talking about; in Ada, the term almost always refers to genérico instantiation. Saying that it "can refer to the creation of an instance of a package or subprogram when speaking of generics" doesn't really answer the question; it doesn't address whether that creación occurs at compile time or run time. - Keith Thompson

Yes. You are quite right. Generic instantiation can occur within any specification -- the declarative portion of a package, subprogram, or task. A generic can be instantiated in the specification of another generic, in which case it takes effect upon its parent's instantiation. Elaboration of a specification, for any construct, at run-time can cause an instantiation to occur -- just as the creation of a constant or initialized variable can occur during elaboration. And yes, this can occur within a loop as NWS indicated. - Eryndlia Mavourneen

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.