Specialization test: fails now, but

Hi, will that code work at some point in the rust future?

#![feature(specialization)]
#![allow(unused)]

trait Trait1{
    fn test(&self);
}
impl Trait1 for u32{
    fn test (&self){
        println!("Trait1")
    }
}
impl Trait2 for u64{
    fn test(&self){
        println!("Trait2")
    }
}
trait Trait2{
    fn test(&self);
}


fn mfn<T: Trait1>(val: T){
    println!("{:?} u32", val.test())
}
fn mfn<T: Trait2>(val: T){
    println!("{:?} u64", val.test())
}
fn main() {
    
    let t = 50_u32;
    mfn(t);
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0428]: the name `mfn` is defined multiple times
  --> src/main.rs:25:1
   |
22 | fn mfn<T: Trait1>(val: T){
   | ------------------------- previous definition of the value `mfn` here
...
25 | fn mfn<T: Trait2>(val: T){
   | ^^^^^^^^^^^^^^^^^^^^^^^^^ `mfn` redefined here
   |
   = note: `mfn` must be defined only once in the value namespace of this module

warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/main.rs:1:12
  |
1 | #![feature(specialization)]
  |            ^^^^^^^^^^^^^^
  |
  = note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information
  = help: consider using `min_specialization` instead, which is more stable and complete
  = note: `#[warn(incomplete_features)]` on by default

For more information about this error, try `rustc --explain E0428`.
warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 1 previous error; 1 warning emitted

#![feature(specialization)] won't become stable in visible future (let's say 5 years). And your code does not use #![feature(specialization)].

(Non arity based) Function overloading will not happen.

If you are trying to unify two strictly non-overlapping trait (disjoint trait), you can use this pattern.

I am no expert but I thought specialization is about overlapping trait implementations not about function overloading.

E.g.

trait Behaviour {}

impl<T> Behaviour for T {}
impl Behaviour for i32 {}

Compiling playground v0.0.1 (/playground)
error[E0119]: conflicting implementations of trait `Behaviour` for type `i32`
 --> src/lib.rs:4:1
  |
3 | impl<T> Behaviour for T {}
  | ----------------------- first implementation here
4 | impl Behaviour for i32 {}
  | ^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `i32`

For more information about this error, try `rustc --explain E0119`.
error: could not compile `playground` (lib) due to 1 previous error

You can see how both trait impls apply to the type i32. Specialization would resolve this ambiguity by somehow making clear that the impl for i32 is more specialized and therefore takes precedence over the generic T impl. I the exact rules are probably still up for debate.

The function overloading that you are asking for I have never seen seriously discussed and I don't think it will happen.

1 Like

Thank you all,

I will emulate it within a trait instead, looks cleaner.

Thanks!