User loginNavigation |
Extension Methods versus Structural Typing of Traits for solving the expression problemHere is a neat little thing that is tying my brain up into knots: Given a Java-like language, what would you prefer Extension Methods (ala C#) or Traits + Structural Typing. My working (read as: probably incomplete) definition of a trait is an interface with concrete methods. Unless I am mistaken the expressiveness of the two techniques would overlap significantly: Example 1 (extension methods):
class A {
int m = 42;
int f() {
return m;
}
}
extension B of A {
int g() {
return f() * 2;
}
}
A x = new A();
print(x.g());
Example 2 (traits + structural-typing)
class A {
int m = 42;
int f() { return m; }
}
trait B {
abstract int f();
concrete int g() { return f() * 2; }
}
B x = new A(); // Notice: A never explicitly "implements" B
print(x.g());
I haven't looked at Scala in a while, so can you do this in it yet? I believe it is a direction they were going. For what its worth, the second approach appeals to me a bit more, because it is more discplined, and doesn't clobber the nice encapsulation advantages of classes. Now if I am not already way out in left-field, are these not both examples of solutions to the "expression problem" (http://www.daimi.au.dk/~madst/tool/papers/expression.txt)? Well I know that in example two I am cheating because there is a cast of "A" to "B". So I would call it a solution to the "weak expression problem". You can guess what I mean by that. By cdiggins at 2009-05-17 21:40 | LtU Forum | previous forum topic | next forum topic | other blogs | 7030 reads
|
Browse archives
Active forum topics |
Recent comments
11 hours 8 min ago
11 hours 23 min ago
5 days 12 hours ago
5 days 12 hours ago
5 days 12 hours ago
3 weeks 6 days ago
4 weeks 4 days ago
4 weeks 4 days ago
4 weeks 6 days ago
4 weeks 6 days ago