User Tools

Site Tools


at:tutorial:appendix

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
at:tutorial:appendix [2008/07/10 16:31] – added tvcutsemat:tutorial:appendix [2008/07/10 17:21] – added tvcutsem
Line 1: Line 1:
-====== Appendix ======+====== Appendix: Libraries ======
  
 In the appendix, we explain useful libraries available to the AmbientTalk/2 programmer. These libraries provide abstractions ranging from traditional, established "collections" up to newly researched language constructs, such as "ambient references". In the appendix, we explain useful libraries available to the AmbientTalk/2 programmer. These libraries provide abstractions ranging from traditional, established "collections" up to newly researched language constructs, such as "ambient references".
Line 536: Line 536:
  
 You can also force a ''StructuralTypeMismatch'' exception to be raised if the object does not conform to the type: You can also force a ''StructuralTypeMismatch'' exception to be raised if the object does not conform to the type:
- *  ''ensure: tom implements: PersonProtocol'' => true or exception +  *  ''ensure: tom implements: PersonProtocol'' => true or exception 
-  ''PersonProtocol.checkConformance(tom)'' => true or exception+  *  ''PersonProtocol.checkConformance(tom)'' => true or exception
  
 More usage examples of structural types can be found in the unit test defined in the file ''at/lang/structuraltypes.at''. More usage examples of structural types can be found in the unit test defined in the file ''at/lang/structuraltypes.at''.
  
 ==== Traits ==== ==== Traits ====
 +
 +The module ''/.at.lang.traits'' exports a small library to use AmbientTalk's traits in a more structured manner. In the literature, traits are described as reusable components with two interfaces: an interface of methods that are //provided// by the trait //to// the composite and an interface of methods that are //required// by the trait //from// the composite. AmbientTalk's traits only make the provided interface explicit. The required interface remains implicit and unchecked at composition time.
 +
 +Using the ''traits'' module, a trait can specify that it requires its composite to adhere to a certain protocol (i.e. a structural type, cf. the previous section). Using traits in this way requires an explicit composition step where the trait's requirements are checked.
 +
 +To define a "structured" trait, define your  trait objects as follows:
 +<code>
 +trait: {
 +  ...
 +} requiring: Protocol;
 +</code>
 +
 +The above code creates a trait that can only be composed into an object adhering to the specified protocol. To compose traits, use the following language construct:
 +
 +<code>
 +object: {
 +  use: {
 +    import T1 exclude ...;
 +    import T2 alias ...;
 +  }
 +}
 +</code>
 +
 +The ''use:'' block can **only** include ''import'' statements. It simply executes the ''import'' statements, but in addition checks whether the composite, //after// having imported all of its traits, provides all of the methods specified in the required protocol of its imported traits.
 +
 +Note that the place where ''use:'' is used inside an object matters: if one of the traits requires a method ''m()'' that is defined only later in the composite, the check will fail. To avoid this, place the ''use:'' block at the bottom of the object declaration.
 +
 +Usage examples can be found in the unit tests in the file ''at/lang/traits.at''.
 +
 +===== Utilities =====
 +
 +The files in the ''at/support'' subdirectory of the standard library implement various utilities of use to the AmbientTalk programmer. We discuss the most useful modules below.
 +
 +==== Timing Utilities ====
 +
 +The module ''/.at.support.timer'' provides utility functions to schedule code for execution at a later point in time. Its most useful control construct is the following:
 +
 +<code>
 +def subscription := when: timeoutPeriod elapsed: {
 +  ...
 +}
 +</code>
 +
 +The ''when:elapsed:'' function takes as its arguments a timeout period (in milliseconds) and a block closure and schedules the closure for execution after the given timeout period. The function returns a subscription object whose single ''cancel'' method can be used to abort the execution of the scheduled code. Once ''cancel'' has been invoked, it is guaranteed that the closure will no longer be executed by the timer module.
 +
 +The milliseconds used to define the timeout period must be provided as a Java ''long'' value. To construct such a value from an AmbientTalk number, the timer module defines the following auxiliary functions:
 +
 +  * ''millisec(ms)'' => convert AmbientTalk number to a Java long value representing a timeout period in milliseconds.
 +  * ''seconds(s)'' => convert AmbientTalk number to a Java long value representing a timeout period in seconds.
 +  * ''minutes(m)'' => convert AmbientTalk number to a Java long value representing a timeout period in minutes.
 +
 +Additionally, the timer module defines a function ''now()'' which returns the current system time as a Java long value.
 +
 +The timer module also defines a function ''whenever:elapsed:'' which repetitively invokes the given block closure every time the timeout period has elapsed. The returned subscription object can be used to eventually stop the repetitive invocation of the closure.
 +
 +The timer module defines a small number of additional utility functions which can be found in the file ''at/support/timer.at''.
 +
 +==== Logging Framework ====
 +
 +==== Object Inspector ====
 +
 +==== Symbiosis Utilities ====
 +
 +==== Miscellaneous ====
at/tutorial/appendix.txt · Last modified: 2021/09/24 10:28 by elisag