Tag Archives: traits

Extending generic types with traits

I am pretty happy I managed to add the feature I was not able to while working on previous Skila — traits. Initially I thought about them as conditional methods:

class Greeter<T>
{
  void hello(T &t) where T : ISay
  {
    t.say();
  }
}

But when reading “Programming Rust” I noticed that syntax for Rust type implementations and this gave me the idea of decoupling those “conditional methods” from the main type:

class Greeter<T>
{
}

trait Greeter<T>
  where T : ISay
{
  void hello(T &t) 
  {
    t.say();
  }
}

It looks very much like extension method however you can spice it with inheritance:

class Greeter<T>
{
}

trait Greeter<T> : ISay
  where T : ISay
{
  void hello(T &t) 
  {
    t.say();
  }
  // traits support polymorphism
  override void say()
  {
    println("just saying");
  }
}

Depending which type you pass when constructing Greeter it will inherit from ISay or not.

At this point there is a little zoo in Skila with similar features — protocols and interfaces, traits and extensions (upcoming) — and I hope some of them will be merged. Simplicity matters.

Tagged , ,

Properties — enough with monkey typing

The more I use properties in C# the more annoyed I am with repetitions. Consider such code:

private SomeType field;
public SomeType Field
{
  get { return this.field; }
  set 
  {
     // some logic with previous value
     this.field = value;
     // some logic with new value
  }
}

Triggering events, logging data in a database, you name it — this pattern occurs over and over again. And the best you can come up is writing Wrapper class and using it like here (still C#):

private Wrapper<SomeType> field = new Wrapper<SomeType>();
public SomeType Field
{
  get { return this.field.value; }
  set { this.field.value = value; }
}

Some advise the solution to this is using snippets which is hilarious idea — it is like saying that you can reduce the amount of code by typing faster. Besides I am not a monkey, so I look for the solution elsewhere.

Somewhere along AOP or Scala-like traits — I am thinking of property specialized overlays:

overlay Wrapper // implicit first generic parameter: T
  def set(value T)
    // some logic with previous value
    inner.set(value);
    // some logic with new value
  end
end

Since overlay will be mixed with property we have to connect it somehow to the already existing setter or getter — all we need is inner reference. And the usage could be reduced to this:

def field SomeType with Wrapper; 

The type of the field would be SomeType, keyword with does not change a type, it just indicates what wiring compiler should do behind the scenes.

It is just a vague idea, but it has to be done, yesterday my inner monkey wrote:

private SomeType field;
public SomeType Field
{
  get { return this.field; }
  set 
  {
     // some logic with new value
     this.field = value;
     // some logic with previous value
  }
}

So it is about time to make properties right.

Tagged , , , ,