Terminology: instance of a Rust type

What would be the best terminology for referring to an "instance of type". This is specifically in the context of a trait documentation. So anything that can implement the trait is what I want to refer to. Is there something more specific than "implementer".

1 Like

I find that phrases such as "any value that implements MyTrait" is used often, even if it's not strictly accurate. Otherwise you can say "any value whose type implements MyTrait" or "any value of a type implementing MyTrait".

2 Likes

I often just use the trait name itself as a direct modifier, for example:

  • “All MyTraits have a foo() function that ...”
  • “The function OtherTrait::bar takes a MyTrait instance and ...”
1 Like

An "instance of a type" would be a value. This comes from OOP speak, where you normally replace type with class and value with object.

From what I can figure out, "anything that can implement the trait" would be an "an instance of the trait". A trait Trait is some predicate, which is fulfilled for a type T when T is implemented for it. Then we write T: Trait. Now C:={T | T: Trait} is the class of all types that fulfill the predicate. Furthermore one can identify Trait with C and speak of a type class in generalized meaning. Then any type implemented for the trait would be considered as an instance.

In practice, I think, the wording "any type with trait Trait" is fine.

1 Like

I personally object to that usage because it's not always clear what "instance of MyTrait" means. It is particularly prone to confusion with the trait object type dyn MyTrait, which for backwards compatibility can still be written as bare MyTrait (although you shouldn't!)

Traits aren't types, so they don't have instances. I suspect "instance of MyTrait" makes more sense to people with a Java or C# background, where interfaces are types, and a statement like "this function takes an instance of IEmployee" has a straightforward and unambiguous meaning. It's not quite so clear-cut in Rust, which often leads to confusion when people try to write code that blurs the lines (like returning different types from a function or putting a type of indeterminate size in a container). It's better to say what you mean.

By all means, clarity is of paramount importance when writing. One of my bad habits is being more precise than the situation warrants, and thereby losing my main point in the excess verbosity.

When the difference between dyn Trait, impl Trait, trait Trait { }, and impl Trait for Something { } is particularly important, I’ve found that using whole sentences to describe what I mean is generally clearer than relying on the reader picking up on small nuances.

All this is a matter of personal writing style, however. There’s no best way to phrase things in general because the goal of writing is to generate a shared understanding between reader and writer— change either of them and the most effective word choice also changes.

I like to use traits as adjectives -- a Copy type, an Iterator type, etc. It doesn't always flow naturally, but English is pretty flexible with its parts of speech. :slightly_smiling_face:

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.