Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: implicit generic function arguments with bounds #11196
Comments
This comment has been minimized.
This comment has been minimized.
|
Besides from the obvious ABI compatibility issue, this proposal severely limits the user's ability to selectively disable monomorphization. For example, if a function |
This comment has been minimized.
This comment has been minimized.
|
The proposal is by no means set in stone, but here's a quick way of disabling monomorphization: fn f_virtual<T: Trait>(x: &T) {
f(x as &Trait)
}
// Maybe this attribute could disable monomorphization by itself?
// Or we could have another attribute, like #[virtual_trait_args].
#[inline(never)]
fn f(x: &Trait) {...} |
This comment has been minimized.
This comment has been minimized.
|
I'd rather have a general solution that interacts well with the rest of the language rather than a special case one (note how T, U, V remain where they are!) that doesn't. I'd look at what other languages with traits/concepts/etc do and see if it's any better. E.g. in D it is also more syntactically light to use dynamic dispatch, and yet most use static dispatch anyway. Here is what a complicated function signature with type constraints looks like in D: void fill(Range1, Range2)(Range1 range, Range2 filler)
if (isInputRange!Range1
&& (isForwardRange!Range2
|| (isInputRange!Range2 && isInfinite!Range2))
&& is(typeof(Range1.init.front = Range2.init.front)))
{
}I.e. the type constraints (the code that starts after the fn map2fn<T, U, V, F1, F2>(x: T, f1: F1, f2: F2) -> V
where<F1: Fn<U, V>, F2: Fn<U, V>>
{
f2(f1(x))
}
// OR
fn map2fn(x: T, f1: F1, f2: F2) -> V
where<T, U, V, F1: Fn<U, V>, F2: Fn<U, V>>
{
f2(f1(x))
} |
This comment has been minimized.
This comment has been minimized.
|
@eddyb, re virtualisation, I believe @lifthrasiir is just pointing out that the proposed syntax only works for traits not contained in any pointers, specifically (and most importantly), you can't write |
This comment has been minimized.
This comment has been minimized.
|
Even without the speculation about |
eddyb
referenced this issue
Jan 14, 2014
Closed
RFC: Limited return type inference constrained to a trait #11455
This comment has been minimized.
This comment has been minimized.
|
I've now written an RFC that includes something like this proposal: rust-lang/rfcs#105 |
rust-highfive
referenced this issue
Sep 24, 2014
Closed
RFC: implicit generic function arguments with bounds #302
This comment has been minimized.
This comment has been minimized.
|
This issue has been moved to the RFCs repo: rust-lang/rfcs#302 |
eddyb commentedDec 29, 2013
Using generic functions with trait bounds results in efficient code generation, at the cost of syntactic simplicity (compared to using trait objects).
Consider this snippet:
Now with trait bounds moved to argument types (creating implicit generic type parameters):
Note that this would be limited to values as references or pointers would conflict with current syntax for trait objects.
However, optimizing
&Traitin an argument type (or even a structure field? there is room for discussion here) to a generic (<T: Trait> ... &T) wouldn't cause any serious issues AFAICT.And it would mean we can make the closure syntax be sugar for
&Fnwithout losing backwards compatibility while allowing compile-time monomorphization.