Boo

composition AST attribute

Details

  • Type: New Feature New Feature
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 0.8.2
  • Fix Version/s: 0.9.5
  • Component/s: Boo.Lang.Useful
  • Labels:
    None
  • Testcase included:
    yes
  • Number of attachments :
    0

Description

Composition is a common design pattern for 'simulating' multiple inheritance.

It would be nice to have a [composeWith(T)] AST attribute in Boo.Lang.Extensions :

Instead of writing manually composition code for code below (ie. duplicated code for each interface implementations) :

interface IAnswer:
    IsAnswer: get

class A:
    [property(X)]
    _x as int

class B(A):
    pass

class A2(A,IAnswer)
    IsAnswer:
        get:
              return X == 42

class B2(B,IAnswer)
    IsAnswer:
        get:
              return X == 42

We could write this :

interface IAnswer:
    IsAnswer: get

class A:
    [property(X)]
    _x as int

class B(A):
    pass

[composeWith(Answer)]
class A2(A):
    pass

[composeWith(Answer)]
class B2(B)
    pass

class Answer(A,IAnswer):
    IsAnswer:
         get:
               return X == 42

The code generated would be :

interface IAnswer:
    IsAnswer: get

class A:
    [property(X)]
    _x as int

    _answer = Answer[of A](self)

class B(A):
    _answer = Answer[of B](self)

class A2(A, IAnswer):
    IsAnswer:
        get:
             return _answer.IsAnswer

class B2(B,IAnswer)
    IsAnswer:
        get:
             return _answer.IsAnswer

class Answer[of T(A,constructor)](IAnswer):
    _a as T
    def constructor(a as T):
          _a = a
    IsAnswer:
         get:
               return _a.X == 42

NB:
1) we need generic constraints for this to work (doing this without generic/constraint would complexify/duplicate code generation as far as virtual/non-virtual members are concerned)
2) i guess this would be cool to use this for IType stuff in TypeSystem when we rewrite boo in boo

Issue Links

Activity

There are no comments yet on this issue.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: