Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a constructor function in a trait?

Tags:

rust

I'm trying to find examples for constructor functions in traits, but haven't had much luck. Is this a idiomatic thing to do in Rust?

trait A {
    fn new() -> A;
}

struct B;
impl A for B {
    fn new() -> B {
        B
    }
}

fn main() {
    println!("message")
}
<anon>:7:8: 9:9 error: method `new` has an incompatible type for trait: expected trait A, found struct `B` [E0053]
<anon>:7        fn new() -> B {
<anon>:8          B
<anon>:9        }
<anon>:7:8: 9:9 help: see the detailed explanation for E0053
error: aborting due to previous error
playpen: application terminated with error code 101

Casting this returns a core::marker::Sized related error.

trait A {
    fn new() -> A;
}

struct B;
impl A for B {
    fn new() -> A {
        B as A
    }
}

fn main() {
    println!("message")
}
<anon>:8:10: 8:16 error: cast to unsized type: `B` as `A`
<anon>:8          B as A
                  ^~~~~~
<anon>:8:10: 8:11 help: consider using a box or reference as appropriate
<anon>:8          B as A
                  ^
<anon>:7:20: 7:21 error: the trait `core::marker::Sized` is not implemented for the type `A + 'static` [E0277]
<anon>:7        fn new() -> A {
                            ^
<anon>:7:20: 7:21 note: `A + 'static` does not have a constant size known at compile-time
<anon>:7        fn new() -> A {
                            ^
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
like image 232
6D65 Avatar asked Aug 08 '15 18:08

6D65


People also ask

Can a trait have a constructor?

Unlike traits in Scala, traits in PHP can have a constructor but it must be declared public (an error will be thrown if is private or protected). Anyway, be cautious when using constructors in traits, though, because it may lead to unintended collisions in the composing classes.

Can Scala trait have constructor?

Scala traits don't allow constructor parameters However, be aware that a class can extend only one abstract class.

Can traits have properties?

Traits can have properties and methods with private and protected visibility too. You can access them like they belong to class itself. There is no difference. Traits can have a constructor and destructor but they are not for the trait itself, they are for the class which uses the trait.

Is a constructor considered a function?

A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.


1 Answers

You need to use the Self type. In trait declarations, Self refers to the type that implements a trait. In your case, the trait declaration should look as follows:

trait A {
    fn new() -> Self; // Self stands for any type implementing A
}

Your original version is subtly different because it will return a trait object, not a value of the implementor type.

like image 127
fjh Avatar answered Sep 20 '22 13:09

fjh