Difference between revisions of "Once"

From Liberty Eiffel Wiki
Jump to navigation Jump to search
(Created page with "==A definition of <TT>once</TT>== A <TT>once</TT> feature is implemented by replacing the '''do''' keyword by '''once''' e. g.: foo: INTEGER is once Result := 1 e…")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==A definition of <TT>once</TT>==
 
==A definition of <TT>once</TT>==
   
A <TT>once</TT> feature is implemented by replacing the '''do''' keyword by '''once''' e. g.:
+
A <TT>once</TT> function is implemented by replacing the '''do''' keyword by '''once''' e. g.:
 
foo: INTEGER is
 
foo: INTEGER is
 
once
 
once
Line 7: Line 7:
 
end
 
end
   
This has the effect, that code of the feature is only executed once in the system lifetime. If the feature is a function (i. e. has a return type) every call of the feature will return the same ''Result'', be it an expanded value or a reference. For references, the references object may change over time, but the once function will always return the identical object.
+
This has the effect, that code of the feature is only executed once in the system lifetime. If the feature is a function (i. e. has a return type) every call of the feature will return the same ''Result'', be it an expanded value or a reference. For references, the references object may change over time, but the once function will always return the identical object. The routines body is executed only once, even if it is called via different names. I. e.:
  +
foo, bar: INTEGER is
  +
once
  +
count := count + 1
  +
Result := count
  +
end
  +
  +
make is
  +
do
  +
assert(foo = 1)
  +
assert(bar = 1)
  +
end
  +
  +
count: INTEGER
   
 
==Further Aspects==
 
==Further Aspects==
  +
* once is not applicable to attributes
 
* ''once "manifest string"''
 
* ''once "manifest string"''
 
* once per object (other eiffel compilers have ''once ("OBJECT")'' to execute the feature body once per object) in Liberty there are no plans to implement such a feature
 
* once per object (other eiffel compilers have ''once ("OBJECT")'' to execute the feature body once per object) in Liberty there are no plans to implement such a feature

Latest revision as of 12:27, 24 March 2013

A definition of once

A once function is implemented by replacing the do keyword by once e. g.:

foo: INTEGER is
   once
     Result := 1
   end

This has the effect, that code of the feature is only executed once in the system lifetime. If the feature is a function (i. e. has a return type) every call of the feature will return the same Result, be it an expanded value or a reference. For references, the references object may change over time, but the once function will always return the identical object. The routines body is executed only once, even if it is called via different names. I. e.:

foo, bar: INTEGER is
   once
      count := count + 1
      Result := count
   end
   
make is
   do
      assert(foo = 1)
      assert(bar = 1)
   end

count: INTEGER

Further Aspects

  • once is not applicable to attributes
  • once "manifest string"
  • once per object (other eiffel compilers have once ("OBJECT") to execute the feature body once per object) in Liberty there are no plans to implement such a feature