Exercises
In the following exercises, you will use ScalaSTM to implement various transactional programming abstractions. In most cases, their implementation will closely resemble a sequential implementation, while using transactions. In some cases, you might need to consult external literature or ScalaSTM documentation to correctly solve the exercise.
- Implement the transactional pair abstraction, represented with the - TPairclass:- class TPair[P, Q](pinit: P, qinit: Q) { def first(implicit txn: InTxn): P = ??? def first_=(x: P)(implicit txn: InTxn): P = ??? def second(implicit txn: InTxn): Q = ??? def second_=(x: Q)(implicit txn: InTxn): Q = ??? def swap()(implicit e: P =:= Q, txn: InTxn): Unit = ??? }- In addition to getters and setters for the two fields, the transactional pair defines the - swapmethod that swaps the fields, and can only be called if types- Pand- Qare the same.
- Use ScalaSTM... 
 
                                             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
     
         
                 
                 
                 
                 
                 
                 
                 
                 
        