On 14/03/2011 8:11 AM, Marijn Haverbeke wrote:
We'll probably keep going off-topic indefinitely here (note the title
of this thread...). I just want to set one thing straight, and then
I'll leave the rest of this conversation for later.
Agreed. For civility I'll let you have the last word after this if you
wish to respond again, but for the record I ought to clarify also (below):
In general we're not supporting (or not *starting* with the notion of
supporting) any transparent conversions at all. Not even subtyping.
Particularly not a conversion that involves allocating a wrapper object and
a vtbl. We don't even auto-box 10 to @int!
Note that no conversion, and thus no implicit conversion, at all
happens in my description. It passes a vtable to the function that is
polymorphic on a typeclass type, doesn't allocate anything. Thus, this
is much more efficient than your wrapper obj example.
On this point, I still think we're talking about the same cost model.
It's true, my choice of the word 'allocation' was an exaggeration, I
meant 'pass a vtbl and possibly indirect through it'. But the same is
true of the example I pasted to Sebastian when I was describing an
obj-encoding of the 'int is an instance of Number' typeclass example
(that incidentally also covers the argument about N-ary ops):
type arith[T] = obj { add(&T a, &T b) -> T;
mul(&T a, &T b) -> T;
// etc.
};
fn addTwice[T](&T a, &T b, arith[T] t) -> T {
ret t.add(t.add(a, b), b);
}
fn main() {
obj int_arith {
fn add(&int a, &int b) -> int { ret a + b; }
fn mul(&int a, &int b) -> int { ret a * b; }
// ...
}
addTwice(10, 11, int_arith());
}
In this case, int_arith is your 'instance declaration'. Our sufficiently
smart compiler (i.e. "LLVM") sees the int_arith() call as forming a
2-word obj with a null box field and a const vtbl. It passes that to
addTwice and, since addTwice is small, the vtbl being passed in is const
and points to const methods that are themselves small, may very well
inline the whole thing down to "10 + 11 + 11".
Same cost model, no?
-Graydon
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev