Home
REScala Reference Manual
Contents
1. Note that events without arguments still need an argument in the handler val e new ImperativeEvent Int e x gt println e x Int gt println Scala allows one to refer to a method using the partially applied function syntax This approach can be used to directly register a method as an event handler For exam ple def m1 x Int val y x 1 println y val e new ImperativeEvent Int e mi _ e 10 3 1 3 Firing Events Events can be fired with the same syntax of a method call When an event is fired a proper value must be associated to the event call Clearly the value must conform the signature of the event For example val el new ImperativeEvent Int val e2 new ImperativeEvent Boolean val e3 new ImperativeEvent Int String O el 10 e2 false e3 10 Hallo we Bw NS we Bw Ne o o NX wR vne When a handler is registered to an event the handler is executed every time the event is fired The actual parameter is provided to the handler val e new ImperativeEvent Int e x gt println x e 10 e 10 output 10 10 If multiple handlers are registered all of them are executed when the event is fired Applications should not rely on any specific execution order for handler execution val e new ImperativeEvent Int e x gt println x e x gt println f n x e 10 e 10 output 10 n 10
2. 0 val v Var 1 val s Signal vO 1 val e Event Int s changed e x Int gt test 1 v set 2 assert test 1 v set 3 assert test 2 4 2 Advanced Conversion Functions Some of the conversion functions can be called on a signal providing an event as the first parameter or can be called on an event providing a signal as the first parameter While the behavior is the same the signature of the function is obviously different For example the function snapshot can return a signal that is updated on an event occurrence Hence the function can be exposed both on the Signal and the Event interface For example val e Event V An event val s Signal V A signal e snapshot V s Signal V Signal LV s snapshot V e Event _ Signal V For simplicity in those cases we document the signature of the function with all the interested objects in the parameters For example def snapshot V e Event _ s Signal V Signal V 4 2 1 Fold The fold function creates a signal by folding events with a given function Initially the signal holds the init value Every time a new event arrives the function f is applied to the previous previous value of the signal and to the value associated to the event The result is the new value of the signal fold T A e Event T init A f A T gt A Signal A Example val e new ImperativeEvent Int val f x Int y Int gt xty val
3. 2 foo c get 15 Bw Ne we BN a 5 4 Cyclic dependencies When a signal s is defined a dependency is establishes with each of the signals or vars that appear in the signal expression of s Cyclic dependencies produce a runtime error and must be avoided For example the following code val a Var 0 WRONG DON T DO IT val s Signal a t val t Signal a s 1 creates a mutual dependency between s and t Similarly indirect cyclic dependen cies must be avoided 5 5 Objects and mutability Vars and signals may behave unexpectedly with mutable objects Consider the follow ing example class Foo init Int WRONG DON T DO IT var x init val foo new Foo 1 val varFoo Var foo 7 val s Signal varFoo x 10 wo 0 wR BN o 0 avoua wNH s get 11 foo x 2 s get 11 One may expect that after increasing the value of foo x in Line 9 the signal ex pression is evaluated again and updated to 12 The reason why the application behaves differently is that signals and vars hold references to objects not the objects them selves When the statement in Line 9 is executed the value of the x field changes but the reference hold by the varFoo var is the same For this reason no change is detected by the var the var does not propagate the change to the signal and the signal is not reevaluated A solution to this problem is to use immutable
4. size specified by the programmer Example val e new ImperativeEvent Int val s Signal List Int e last 5 11 Anes Ak Bw Ne a u RD Cm IN a assert s get List e 1 assert s get List 1 e 2 assert s get List 1 2 e 3 e 4 e 5 assert s get List 1 2 3 4 5 e 6 assert s get List 2 3 4 5 6 4 2 5 List Collects the event values in a growing list This function should be used carefully Since the entire history of events is maintained the function can potentially introduce a memory overflow list T e Event T Signal List T 4 2 6 Count Returns a signal that counts the occurrences of the event Initially when the event has never been fired yet the signal holds the value 0 The argument of the event is simply discarded count e Event Signal Int val e new ImperativeEvent Int val s Signal Int e count assert s get 0 e 1 e 3 assert s get 2 4 2 7 Snapshot Returns a signal updated only when e fires If s in the meanwhile changes its value the change is ignored When the event e fires the resulting signal is updated to the current value of s snapshot V e Event s Signal V Signal V Example val e new ImperativeEvent Int val v Var 1 val si Signal vO 1 val s e snapshot s1 e 1 assert s get 2 v set 2 assert s get 2 e 1 assert s get 3 12 w n ie Vo IDO apaGLD
5. value 2 and is not updated when the var changes val a Var 1 val b increment a get val s Signal b 1 The following solution is syntactically correct and the signal s is updated every time the var a is updated val a Var 1 val s Signal increment a 1 17 6 Technicalities This section is meant to cover the implementation details of RESCALA that are necessary to correctly run the current the library 6 1 Imports and dependencies To work with RESCALA programmers need to properly import the reactive abstractions offered by the language The following imports are normally sufficient for all the basic functionalities of RESCALA import rescala _ import rescala events _ import makro SignalMacro SignalM gt Signal Note that signal expressions are currently implemented as macros i e the body of a signal expression is analyzed to detect the reactive values and establish the de pendencies To use macros for signal expressions the macro SignalM is imported and renamed to Signal Line 3 18 7 Essential Related Work A more academic presentation of RESCALA is in 7 A complete bibliography on reactive programming is beyond the scope of this work The interested reader can refer to 1 for an overview of reactive programming and to 8 for the issues concerning the integration of RP with object oriented programming RESCALA builds on ideas originally developed in EScala 3 which supports event
6. 11 ADA CLAS Es ia Mik be BSB se LS I Re ae Bk 11 425 TASt e Ue i ek ek eden bh AO a Bot doe a nA dob are A 12 4 207 Count 8 eo eke oe st Ba aS 12 ADA DAPSONE OR APES OE oe AE EAR GES 12 428 Change oir whe BSS SEA eek Pe eae eS 13 4 2 9 gt Changed To sc fe we Fader bak ie Sed ee hws 13 AD NOD Reset i fa e tel ae 2s IE OR a 13 4 2 11 SWitch t gele o o ee ee ee aap Re e a 14 A212 UD WIAD tes as Pee hw ee A Sh eee A Oe eed 14 5 Common Pitfalls 15 5 1 Accessing values in signal expressions 15 5 2 Attempting to assign a signal o 15 5 3 Side effects in signal expressions 15 5 4 Cyclic dependencies e o 16 5 5 Objects and mutability o o e 5 6 Functions of reactive values Technicalities 6 1 Imports and dependencies o Essential Related Work Acknowledgments II 18 18 19 20 1 Introduction This manual covers the main features of the RESCALA programming language Sec tion 2 presents time changing values in RESCALA Section 3 describes events Section 4 covers the conversion functions between events and time changing values Section 6 presents technical details that are necessary to correctly run RESCALA Section 7 out lines the related work Intended audience and prerequisites This manuscript is mainly intended for stu dents who approach reactive programming in S
7. 2009 G Salvaneschi G Hintz and M Mezini REScala Bridging between object oriented and functional style in reactive applications In Proceedings of the 13th International Conference on Aspect Oriented Software Development AOSD 14 New York NY USA Accepted for publication 2014 ACM G Salvaneschi and M Mezini Reactive behavior in object oriented applications an analysis and a research roadmap In Proceedings of the 12th annual inter national conference on Aspect oriented software development AOSD 13 pages 37 48 New York NY USA 2013 ACM Scala site http www scala lang org 21
8. For example the following signal declaration creates a dependency between a and s and a dependency between b and s val s Signal a bO The following code instead establishes only a dependency between b and s val s Signal a get b In other words in the last example if ais updated s is not automatically updated With the exception of the rare cases in which this behavior is desirable using get inside a signal expression is almost certainly a mistake As a rule of dumb signals and vars appear in signal expressions with the operator 5 2 Attempting to assign a signal Signals are not assignable Signal depends on other signals and vars the dependency is expressed by the signal expression The value of the signal is automatically updated when one of the values it depends on changes Any attempt to set the value of a signal manually is a mistake 5 3 Side effects in signal expressions Signal expressions should be pure i e they should not modify external variables For example the following code is conceptually wrong because the variable c is impera tively assigned form inside the signal expression Line 4 var c WRONG DON T DO IT x 0 val s Signal val sum a bQ c sum 2 foo c A possible solution is to refactor the code above to a more functional style For example by removing the variable c and replacing it directly with the signal val c Signal val sum a b sum
9. atb printin a b c gt 2 3 5 a 4 println a b c gt 2 4 5 7 atb o wR BN a Rw Ny println a b c 4 3 7 Line 3 specifies the value of c as a function of a and b Since Line 3 defines a statement the relation c a 6 is valid after the execution of Line 3 Clearly when the value of a is updated the relation c a bis not valid anymore Line 6 To make sure that the relation still holds the programmer needs to recompute the expression and reassign c like in line 7 Reactive programming and RESCALA provide abstractions to express constraints in addition to statements In RESCALA the programmer can specify that the constraint c a b always holds during the execution of a program Every time a or b change the value of c is automatically recomputed For example val a Var 2 val b Var 3 val c Signal a b println a get b get c get gt 2 3 5 aQ 4 println a get b get c get gt 4 3 7 In the code above the signal in Line 3 defines the constraint c a b When one of the reactive values involved in the constraint is updated Line 5 the expression in the constraint is recomputed behind the scenes and the value of a is automatically updated As the reader may have noticed expressing constraints in RESCALA requires to conform some syntactic conventions which are discussed in the next sections 2 1 Vars 2 1 1 Defining Vars Programmers express reactiv
10. behaves as follows Every 20 milliseconds the value of the time var is increased by 1 Line 9 When the value of the time var changes the signal expression at Line 3 is reevaluated and the value of space is updated Finally the current value of the space signal is printed every time the value of the signal changes Printing the value of a signal deserves some more considerations Technically this is achieved by converting the space signal to an event that is fired every time the signal changes its value Line 5 The conversion is performed by the changed operator The operator attaches an handler to the event returned by the changed operator When the event fires the handler is executed Line 5 is equivalent to the following code val e Event Int space changed val handler Int gt Unit x Int gt println x e handler Note that using println space get would also print the value of the signal but only at the point in time in which the print statement is executed Instead the approach described so far prints all values of the signal More details about converting signals into events and back are provided in Section 4 wR BN aa Awe Bw N 3 Events RESCALA supports different kind of events Imperative events are directly triggered from the user Declarative events trigger when the events they depend on trigger In reactive applications events are typically used to model changes that happen at discrete points in
11. combination and implicit events Other reactive languages directly represent time changing values and remove inversion of control Among the others we mention Fr Time 2 Scheme FlapJax 6 Javascript AmbientTalk R 4 and Scala React 5 Scala 8 Acknowledgments Several people contributed to this manual with their ideas and comments Among the others Gerold Hintz and Pascal Weisenburger 20 References 1 9 E Bainomugisha A Lombide Carreton T Van Cutsem S Mostinckx and W De Meuter A survey on reactive programming ACM Comput Surv To ap pear 2013 G H Cooper and S Krishnamurthi Embedding dynamic dataflow in a call by value language In ESOP pages 294 308 2006 V Gasiunas L Satabin M Mezini A N ez and J Noy EScala modular event driven object interactions in Scala AOSD 711 pages 227 240 ACM 2011 A Lombide Carreton S Mostinckx T Cutsem and W Meuter Loosely coupled distributed reactive programming in mobile ad hoc networks In J Vitek editor Objects Models Components Patterns volume 6141 of Lecture Notes in Com puter Science pages 41 60 Springer Berlin Heidelberg 2010 I Maier and M Odersky Deprecating the Observer Pattern with Scala react Tech nical report 2012 L A Meyerovich A Guha J Baskin G H Cooper M Greenberg A Bromfield and S Krishnamurthi Flapjax a programming language for ajax applications OOPSLA 09 pages 1 20 ACM
12. 10 n 10 3 1 4 Unregistering Handlers Handlers can be unregistered from events with the operator When a handler is unregistered it is not executed when the event is fired val e new ImperativeEvent Int val handleri x Int gt println x val handler2 x Int gt println s n x e handleri e handler2 7 e 10 e handler2 e 10 e handler1 e 10 output 10 n 10 10 3 2 Declarative Events RESCALA supports declarative events which are defined as a combination of other events For this purpose it offers operators like e lez e amp amp p e map f Event composition allows to express the application logic in a clear and declarative way Also the update logic is better localized because a single expression models all the sources and the transformations that define an event occurrence 3 2 1 Defining Declarative Events Declarative events are defined by composing other events The following code snippet shows some examples of valid definitions for declarative events WR BN we Bw NS a 7 output wk Bw Ne we BN val el new ImperativeEvent Int val e2 new ImperativeEvent Int val e3 el e2 val e4 el amp amp x Int gt x gt 10 val e5 el map x Int gt x toString 3 3 Event Operators This section presents in details the operators that allow one to compose events into declarative events 3 3 1 OR Events The ev
13. REScala Reference Manual Guido Salvaneschi Technical University of Darmstadt salvaneschi informatik tu darmstadt de May 2015 Version 0 3 Contents 1 Introduction 1 2 Signals and Vars 2 2 A 28 2 2 1 1 Defining ValS oe a Be ed 2 2 1 2 Assigning Vars noes o Se E a E 3 227 A e nae Ges bs ee Ge sds egy O O 3 2 2 1 Defining Signals 3 2 2 2 Signalexpressions 2 02 0004 3 2 2 3 Accessing reactive values o 4 2 3 Example Speed coso eche e ek ee oe ee 4 3 Events 5 3 1 Imperative events e 5 3 1 1 Defining EventS o e e e 5 3 1 2 Registering Handlers o o 6 3313 Firin Events ys ee Oe le risa o E 6 3 1 4 Unregistering Handlers o 7 3 2 Declarative Events ia a ele A 7 3 2 1 Defining Declarative Events 7 3 3 Event Operators Si on sled Se PO ee yee eG as 8 3 3 1 OR Events coi Ba ee Ab ee ea eo 8 3 3 2 Predicate Events o o e 8 3337 Map EVENS e soso e e k 8 4 Conversion Functions 9 4 1 Basic Conversion Functions o 9 4 1 1 Event to Signal Latest 9 4 1 2 Signal to Event Changed 9 4 2 Advanced Conversion Functions o 10 E A cg eb eh hs dee ok ee 10 4 22 Mera o 64 aii bn bite Ad be a eel a teh 11 4 2 3 LatestOpuon pos eee a Sieh ek woe et
14. at interface signals and events Conversion functions are fundamental to introduce time changing values into OO applications which are usually event based 4 1 Basic Conversion Functions This section covers the basic conversions between signals and events Figure 1 shows how basic conversion functions can bridge signals and events Events Figure 1 left occur at discrete point in time x axis and have an associate value y axis Signals instead hold a value for a continuous interval of time Figure 1 right The latest con version functions creates a signal from an event The signal holds the value associated to an event The value is hold until the event is fired again and a new value is available The changed conversion function creates an event from a signal The function fires a new event every time a signal changes its value 4 1 1 Event to Signal Latest Returns a signal holding the latest value of the event e The initial value of the signal is set to init latest T e Event T init T Signal T Example val e new ImperativeEvent Int val s Signal Int e latest 10 assert s get 10 e 1 assert s get 1 e 2 assert s get 2 e 1 assert s get 1 4 1 2 Signal to Event Changed The changed function applies to a signal and returns an event that is fired every time the signal changes its value wR Bw N Von wR BN e wR BN a changed U gt T Event U Example var test
15. atively between the given signals on the occurrence of an event e The value attached to the event is simply discarded toggle T e Event a Signal T b Signal T Signal T The switchTo function switches the value of the signal on the occurrence of the event e The resulting signal is a constant signal whose value is the value carried by the event e switchTo T e Event T original Signal T Signal T The switchOnce function switches to a new signal provided as a parameter once on the occurrence of the event e switchOnce T e Event _ original Signal T newSignal Signal T Signal T 4 2 12 Unwrap The unwrap function is used to unwrap an event inside a signal def unwrap T wrappedEvent Signal Event T Event T It can for instance be used to detect if any signal within a collection of signals fired a changed event 1 val collection List Signal _ 2 val innerChanges Signal collection map _ changed reduce a b gt a b 3 val anyChanged innerChanges unwrap 14 1 1 1 2 3 4 5 6 5 Common Pitfalls In this section we collect the most common pitfalls for users that are new to reactive programming and RESCALA 5 1 Accessing values in signal expressions The operator used on a signal or a var inside a signal expression returns the sig nal var value and creates a dependency The get operator returns the current value but does not create a dependency
16. cala for the first time The manual assumes basic knowledge of the Scala 9 language and of functional programming high order functions anonymous functions etc No previous knowledge of reactive programming is assumed While a major aspect of RESCALA s design is the integration of events and signals they can be used separately For example a programmer can use only RESCALA events to design application that do not need time changing values Scope The manual covers the basic features of RESCALA Some functionalities in cluding implicit events and high order signals are intentionally not covered other like event polymorphism are only sketched More details can be found in 7 3 The manual introduces the concepts related to functional reactive programming and event based programming from a practical perspective The readers interested in a more general presentation of these topics can find in Section 7 the essential references wR BN a 2 Signals and Vars A signal is language concept for expressing functional dependencies among values in a declarative way Intuitively a reactive value can depend on variables sources of change without further dependencies or on other reactive values When any of the dependency sources changes the expression defining the reactive value is automati cally recomputed by the language runtime to keep the reactive value up to date Consider the following example vara 2 var b 3 varc
17. e computations starting from vars Vars wrap normal Scala values For example Var 2 creates a var with an Int value and initializes the var to the value 2 Vars are parametric types A var that carries integer values has type Var Int The following code snippet shows valid var declarations val a Var 0 val b Var Hello World val c Var false val d Var Int Var 30 o u Lon wR BN Bow Ne val e Var String Var REScala val f Var Boolean Var false 2 1 2 Assigning Vars Vars can be directly modified with the operator For example v 3 replaces the current value of the v var with 3 Therefore vars are changed imperatively by the programmer 2 2 Signals 2 2 1 Defining Signals Signals are defined by the syntax Signal sigexpr where sigexpr is a side effect free expression Signals are parametric types A signal that carries integer values has the type Signal Int 2 2 2 Signal expressions When inside a signal expression defining a signal s a var or a signal is called with the O operator the var or the signal are added to the values s depends on In that case s is a dependency of the vars and the signals in the signal expression For example in the code snippet val a Var 0 val b Var 0 val s Signal a b Multiple vars in a signal expression The signal s is a dependency of the vars a and b meaning that the values of s depends on both a and b The following code snipp
18. ent e lez is fired upon the occurrence of one among e or ez Note that the events that appear in the event expression must have the same parameter type Int in the next example val el new ImperativeEvent Int val e2 new ImperativeEvent Int val e1_0R_e2 el e2 el_0R_e2 x Int gt println x e1 1 e2 2 1 2 3 3 2 Predicate Events The event e amp amp p is fired if e occurs and the predicate p is satisfied The predicate is a function that accepts the event parameter as a formal parameter and returns Boolean In other words the amp amp operator filters the events according to their parameter and a predicate val e new ImperativeEvent Int val e_AND Event Int e amp amp x Int gt x gt 10 e AND x Int gt println x e 5 e 15 output 15 3 3 3 Map Events The event e mapf is obtained by applying f to the value carried by e The map func tion must take the event parameter as a formal parameter The return type of the map function is the type parameter value of the resulting event val e new ImperativeEvent Int val e MAP Event String e map x Int gt x toString e MAP x String gt println s Here x e 5 e 15 output 7 Here 5 o Here 15 We BN wo 0 2 latest 3 1 m eae I changed event signal Figure 1 Basic conversion functions 4 Conversion Functions RESCALA provides functions th
19. ets define valid signal declarations val a Var 0 val b Var 0 val c Var 0 val r Signal Int Signal a 1 Explicit type in var decl val s Signal aQ b Multiple vars is a signal expression val t Signal s c 10 Miz signals and vars in signal expressions val u Signal s x tQ A signal that depends on other signals val a Var 0 val b Var 2 val c Var true val s Signal if c a else b def factorial n Int val a Var 0 val s Signal Int Signal A signal expression can be any code block val tmp a gt 2 val k factorial tmp k 2 Returns an Int 2 2 3 Accessing reactive values The current value of a signal or a var can be accessed using the get method For example 1 val a Var 0 2 val b Var 2 3 val c Var true 4 val s Signal Int Signal a bO 5 val t Signal Boolean Signal c 6 val x Int a get 7 val y Int s get s val z Boolean t get 9 println z we Ne 2 3 Example speed The following example computes the displacement space of a particle that is moving at constant speed SPEED The application prints all the values associated to the displace ment over time val SPEED 10 val time Var 0 val space Signal SPEED time space changed x Int gt println x while true Thread sleep 20 time time get 1 output 10 20 30 40 The application
20. o o 3 11 4 2 8 Change The change function is similar to changed but it provides both the old and the new value of the signal in a tuple change U gt T Event U U Example val s Signal val e Event Int Int s change e x Int Int gt Y 4 2 9 ChangedTo The changedTo function is similar to changed but it fires an event only when the signal changes its value to a given value changedTo V value V Event Unit var test 0 val v Var 1 val s Signal vO 1 val e Event Unit s changedTo 3 e x Unit gt test 1 assert test 0 v set 2 assert test 1 v set 3 assert test 1 4 2 10 Reset When the reset function is called for the first time the init value is used by the fac tory to determine the signal returned by the reset function When the event occurs the factory is applied to the event value to determine the new signal reset T A e Event T init T factory T gt Signal A Signal A Example val e new ImperativeEvent Int val vi Var 0 val v2 Var 10 val si Signal vi 1 val s2 Signal v2 1 def factory x Int x42 match case 0 gt s1 case 1 gt s2 val s3 e reset 100 factory assert s3 get 1 vi set 1 13 15 assert s3 get 2 16 e 101 17 assert s3 get 11 18 v2 set 11 19 assert s3 get 12 4 2 11 Switch toggle The toggle function switches altern
21. objects Since the objects cannot be modified the only way to change a filed is to create an entirely new object and assign it to the var As a result the var is reevaluated class Foo x Int val foo new Foo 1 val varFoo Var foo val s Signal varFoo x 10 s get 11 varFoo newFoo 2 s get 12 Alternatively one can still use mutable objects but assign again the var to force the reevaluation However this style of programming is confusing for the reader and should be avoided when possible class Foo init Int WRONG DON T DO IT var x init val foo new Foo 1 val varFoo Var foo val s Signal varFoo x 10 16 wo 0 DON vne Noe s get 11 foo x 2 varFoo foo s get 11 5 6 Functions of reactive values Functions that operate on traditional values are not automatically transformed to oper ate on signals For example consider the following functions def increment x Int Int gt Int x 1 The following code does not compile because the compiler expects an integer not a var as a parameter of the increment function In addition since the increment function returns an integer b has type Int and the call b in the signal expression is also rejected by the compiler val a Var 1 WRONG DON T DO IT x val b increment a val s Signal b 1 The following code snippet is syntactically correct but the signal has a constant
22. s Signal Int e fold 10 f e 1 e 2 assert s get 13 10 wR WBN 2000 u BR Bw N Cm Zn Noe 4 2 2 Iterate Returns a signal holding the value computed by f on the occurrence of an event Differ ently from fold there is no carried value i e the value of the signal does not depend on the current value but only on the accumulated value iterate A e Event _ init A f A gt A Signal A Example var test Int 0 val e new ImperativeEvent Int val f x Int gt test x x 1 val s Signal Int e iterate 10 f e 1 assert test 10 assert s get 11 e 2 assert test 11 assert s get 12 e 1 assert test 12 assert s get 13 4 2 3 LatestOption The latestOption function is a variant of the latest function which uses the Option type to distinguish the case in which the event did not fire yet Holds the latest value of an event as Some val or None latestOption T e Event T Signal Option T Example val e new ImperativeEvent Int val s Signal Option Int e latestOption assert s get None e 1 assert s get Option 1 e 2 assert s get Option 2 e 1 assert s get Option 1 4 2 4 Last The last function generalizes the latest function and returns a signal which holds the last n events last T e Event T n Int Signal List T Initially an empty list is returned Then the values are progressively filled up to the
23. s is easily accom plished by using a tuple as a generic parameter type For example val el new ImperativeEvent Int Int O val e2 new ImperativeEvent String String O val e3 new ImperativeEvent String Int O val e4 new ImperativeEvent Boolean String Int val e5 ImperativeEvent Int Int new ImperativeEvent Int Int O Note that an imperative event is also an event Therefore the following declaration is also valid val el Event Int new ImperativeEvent Int Vo eo wR BN o 0 wn e wk uv Ne x wR BN a 3 1 2 Registering Handlers Handlers are code blocks that are executed when the event fires The operator at taches the handler to the event The handler is a first class function that receives the attached value as a parameter The following are valid handler definitions var state 0 val e new ImperativeEvent Int e println _ e x gt println x e x Int gt println x e x gt Multiple statements in the handler state x println x p The signature of the handler must conform the signature of the event since the handler is supposed to process the attached value and perform side effects For example is the event is of type Event Int Int the handler must be of type Int Int gt Unit val e new ImperativeEvent Int String O e x gt println x _1 println x _2 p e x Int String gt println x p
24. time For example a mouse click from the user or the arrival of a new network packet Some features of RESCALA events are valid for all event types e Events carry a value The value is associated to the event when the event is fired and received by all the registered handlers when each handler is executed e Events are generic types parametrized with the type of value they carry like Event T and ImpertiveEvent T where T is the value carried by the event e Both imperative events and declarative events are subtypes of Event T and can referred to generically 3 1 Imperative events RESCALA imperative events are triggered imperatively by the programmer One can think to imperative events as a generalization of a method call which supports multi ple bodies that are registered and unregistered dynamically 3 1 1 Defining Events Imperative events are defined by the ImperativeEvent T type The value of the pa rameter T defines the value that is attached to the event An event with no parameter attached has signature ImpertiveEvent Unit The following code snippet show valid events definitions val el new ImperativeEvent Unit val e2 new ImperativeEvent Int val e3 new ImperativeEvent String val e4 new ImperativeEvent Boolean val e5 ImperativeEvent Int new ImperativeEvent Int class Foo val e6 new ImperativeEvent Foo It is possible to attach more than one value to the same event Thi
Download Pdf Manuals
Related Search
Related Contents
Devolo dLAN 200 AVeasy Aquasplash - Multipure.com User Guide Samsung 700DX-3 Benutzerhandbuch Roberts Gorden UHA SERIES 150 User's Manual ISL45041EVAL1Z User Guide 3-4RSM Guide U-1 Pro Dickson Industrial SM720/SM725 User's Manual Mr. Coffee BVMC-KG2 Coffeemaker User Manual Copyright © All rights reserved.
Failed to retrieve file