Guest User

Untitled

a guest
Jan 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. class Attribute[C[_], E]
  2.  
  3. class Id[E]
  4.  
  5. trait Entity {
  6. val att1: List[Int]
  7. val att2: Int
  8. }
  9.  
  10. object Entity {
  11. val att1reif = new Attribute[List, Int]
  12. val att2reif = new Attribute[Id, Int]
  13. }
  14.  
  15. def Let[T <: Entity, C[_], E](en: T, att: Attribute[C, E], ce: C[E]): T =
  16. /* Updates the whole attribute */
  17.  
  18. def Let[T <: Entity, C[_], E](en: Entity, att: Attribute[C, E], e: E, mode: Boolean): T =
  19. /* Adds or removes (mode) an item */
  20.  
  21. val ent = new Entity { val att1 = List(); val att2 = 3 }
  22. Let(ent, Entity.att1reif, List(1, 2, 3)) // att1 = List(1, 2, 3)
  23. Let(ent, Entity.att1reif, 4, true) // att1 = List(1, 2, 3, 4)
  24. Let(ent, Entity.att1reif, 1, false) // att1 = List(2, 3, 4)
  25.  
  26. // Same code as DSL
  27. ent.att1 := List(1, 2, 3)
  28. ent.att1 :+ 4
  29. ent.att1 :- 1
  30.  
  31. trait AttributeHelper {
  32. type T <: Entity
  33. type C[_]
  34. type E
  35. val ent: T
  36. val att: Attribute[C, E]
  37. def :=(ce: C[E]): T = Let(ent, att, ce)
  38. def :+(e: E): T = Let(ent, att, e, true)
  39. def :-(e: E): T = Let(ent, att, e, false)
  40. }
  41.  
  42. def toAttributeHelperImpl[V: c.AbsTypeTag](c: Context)(expr: c.Expr[V]): c.Expr[AttributeHelper] =
  43. /* A looong macro (currently broken), since I can't split V into C[_] and E,
  44. * which are needed to generate the expression that instantiates an *AttributeHelper*.
  45. * The macro is responsible of finding the attribute reification.
  46. */
  47.  
  48. implicit def toAttributeHelper[V](expr: V): AttributeHelper = macro toAttributeHelperImpl[V]
Add Comment
Please, Sign In to add comment