Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There should be a Printable trait all primitive types inherit by default. #574

Closed
Chilinot opened this issue Oct 2, 2016 · 8 comments
Closed
Assignees

Comments

@Chilinot
Copy link

Chilinot commented Oct 2, 2016

There should be a "Printable" trait that all objects can implement, and that all primitive datatypes inherit by default. This would enable me to define that a given argument has to be printable. Maybe it should be done like or similar to the way Rust handles it.

@TobiasWrigstad
Copy link
Contributor

It would be good to have a short description of how Rust does it in this issue!

In general I agree with this -- I have somewhere at some point proposed a trait with such a function. I dislike names like Printable and Show, and prefer something that talks about the string representation of something. But this is a minor point.

@Chilinot
Copy link
Author

Chilinot commented Oct 4, 2016

Another note, the print functions should require this trait. All objects that would like to be printable has to implement it. That way you only need to pass a reference to the object when you want to print it.

@Chilinot
Copy link
Author

Chilinot commented Oct 4, 2016

This is a stolen example of how rust does it.
Source: http://stackoverflow.com/a/36439447

struct MyStruct {
    a: i32,
    b: i32
}

impl std::fmt::Display for MyStruct {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "(value a: {}, value b: {})", self.a, self.b)
    }
}

fn main() {
    let test = MyStruct { a: 0, b: 0 };

    println!("Used Display: {}", test);    
}

@helanhalvan
Copy link
Contributor

helanhalvan commented Oct 5, 2016

The thing which seems easiest to understand (and implement for new programmers) is probably something like a "serialize" function, which returns a string representation of the object. So foo.serialize() returns a string and print(foo) de-sugars to something like print(foo.serialize()).

Seems nicer then that middle bit of what rust does. Also, then it's possible to have a default implementation for all objects (concatenating all their fields which are serialize-able in some way).

@Chilinot
Copy link
Author

Chilinot commented Oct 5, 2016

That is pretty much what the Rust example does. Except it directly writes to a given reference instead of returning a String. The middle bit is the user implementation of the trait.

@helanhalvan
Copy link
Contributor

Yea, its the writing to a reference bit I think would be nice to avoid.

@kaeluka
Copy link
Contributor

kaeluka commented Mar 1, 2017

Some of the infrastructure that gives methods to maybes, futures, arrays in #698 could possibly be used to realise this.

@EliasC
Copy link
Contributor

EliasC commented May 4, 2017

Subsumed by #797 and #799.

@EliasC EliasC closed this as completed May 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants