Prev: Lost in translation (with SPARK user rules)
Next: Sockets package in SPARK (Was: Lost in translation (with SPARK user rules))
From: Alex R. Mosteo on 10 Jun 2010 09:58 Georg Bauhaus wrote: > On 08.06.10 12:10, Niklas Holsti wrote: >> Martin Krischik wrote: >> >>> just about as painful as using local packages to add static variables >>> to Ada procedures. >> >> Later Martin Krischik wrote: >> >>> function f returns Integer >>> >>> package i is >>> g : Integer := 0; >>> end i; >>> >>> begin >>> i.g := i.g + 1; >>> return i.g; >>> end f; >> >> If you mean the latter code to be an example of the former task (add >> static variable), I believe you are mistaken. > > I thought, too, that a new variable decl inside the local package > would still be elaborated each time. The initilization of g > confirms when tested. > > But then I thought, surprise!, doubted everything I thought > I had learned about Ada. Looking at this: > > with Ada.Text_IO; use Ada.Text_IO; > > procedure Sttc is > > type T is range 0 .. 10; > > procedure A is > package P is -- local package > V: T; -- presumably static? > end P; > begin > if P.V'Valid then > P.V := P.V + 1; > else > P.V := T'First; > end if; > Put_Line (T'Image(P.V)); > end A; > > begin > A; A; A; > end Sttc; > > > $ ./sttc > 0 > 1 > 2 > 3 > $ > > Compiled with or without -gnatVa. But this seems to be accidental, > V is just a variable that isn't initialized, living on the stack. > Confirmation: > > with Ada.Text_IO; use Ada.Text_IO; > > procedure Sttc is > > type T is range 0 .. 10; > > procedure A is > package P is > V: T; > end P; > begin > if P.V'Valid then > P.V := P.V + 1; > else > P.V := T'First; > end if; > Put_Line (T'Image(P.V)); > end A; > > procedure Ovrwrt_Stk is > X : T; > begin > X := T'First; > end Ovrwrt_Stk; > > begin > A; Ovrwrt_Stk; A; Ovrwrt_Stk; A; Ovrwrt_Stk; A; > end Sttc; > > $ ./sttc > 0 > 1 > 1 > 1 > $ > > Phew. Back to normal. Hehehe here too :) Nice example BTW with the stack.
From: J-P. Rosen on 10 Jun 2010 10:29 Alex R. Mosteo a �crit : > Martin Krischik wrote: > > (...) > >> function f returns Integer >> >> package i is >> g : Integer := 0; >> end i; >> >> begin >> i.g := i.g + 1; >> return i.g; >> end f; > > Do you mean that i.g retains value across calls here? Never saw this > construct before, and not what I would have thought... interesting. No! In Ada, no construct lives longer than its scope (unlike C static variables) -- --------------------------------------------------------- J-P. Rosen (rosen(a)adalog.fr) Visit Adalog's web site at http://www.adalog.fr
From: Yannick Duchêne (Hibou57) on 10 Jun 2010 14:21 Le Tue, 08 Jun 2010 12:10:38 +0200, Niklas Holsti <niklas.holsti(a)tidorum.invalid> a écrit: > To make a statically allocated variable, you need a library package. > Perhaps you are thinking of the technique suggested by Mark Lundquist, > at http://www.adapower.com/index.php?Command=Class&ClassID=Basics&CID=204 > > That technique uses a nested package inside a library package, but not > within the subprogram using the variable. The nesting is used only to > hide the variable from other subprograms that are in the same library > package but are not in the nested package. The general technique is a construct where a package (the one holding the variable) is elaborated only once. -- There is even better than a pragma Assert: a SPARK --# check. --# check C and WhoKnowWhat and YouKnowWho; --# assert Ada; -- i.e. forget about previous premises which leads to conclusion -- and start with new conclusion as premise.
From: Vadim Godunko on 10 Jun 2010 15:18 On Jun 10, 2:12 pm, Georg Bauhaus <rm.dash-bauh...(a)futureapps.de> wrote: > > Will it, in theory, be possible to write a Qt-like library > in Ada such that client programs declare variables like > > W : Some_Window'Class := Some_Lib_Pack.Make (Typ => Some_Id); > > That is, programs do not use access Some_Window'Class? Unfortunately, not now. All you can use now is instantiation of Qt_Ada.Generic_Object_Pointers as "container" for your object. This package is instantiated for every subclass derived from QObject. This generic introduces interesting idea of managing referenced object's life cycle, stored reference is automatically resetted to null when object is destroyed. By the way, ARG can think about such functionality in the next standard of the language, for example by returning back reference semantic for limited tagged types and their classwide companions. I think that this capability can significantly improve usage of the language outside of embedded system sector. And as gigantic example to test new language capabilities ARG can use... UML metamodel, it contains most kinds (and may be even all) of complex relations between classes.
From: Simon Wright on 10 Jun 2010 15:33
Maciej Sobczak <see.my.homepage(a)gmail.com> writes: > Well, not so fast. Access types are necessary for referring to > existing objects. And for passing around limited objects; the example I have in mind is events in an event-driven system. Some description ... http://coldframe.sourceforge.net/coldframe/event-use.html#postinganevent |