Perhaps as an FYI, but hopefully useful over time as Rust continues to expand its type capacity.
A phantom type does infer meaning. The classic example of a phantom type is how it can be used to define a Constant
. Sketching it out in Haskell it looks something like:
newtype Const A B = Const { get_const:: A }
// B is the phantom type
The left side is what the type system sees, the right side the runtime. I have not seen this capacity play out in Rust (as of yet anyway). Being mostly new to Rust, I gather that explicit type annotations and traits are ways to influence a type without necessarily changing the value. Notwithstanding...
A classic use case is "tagging" whether a user application has been "approved". Only the function with the credentials to change the type (Form InProcess -> Form Approved
) controls that process. All made possible by the compiler; runtime is guaranteed to "follow the rules" whilst only "seeing" the instantiated Form
type. I have used it to track the difference between "a data request" and "the data itself"; one is a wish, the other what exists in reality (if you will).
All this to say, whether to truly call the "tag", "phantom", depends on how much what is going on in your use of the "tag" matches that of a "phantom" type... a term with meaning in a type system. The benefits of using the verbose version: you tap into the understanding of how that pattern is expected to work.
Notwithstanding all of the above,
marker: PhantomData
... from a def that one is likely to visit at least once in their lifetime; the LinkedList
module...
#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter(&self) -> Iter<'_, T> {
Iter { head: self.head, tail: self.tail, len: self.len, marker: PhantomData }
}
Don't think, just copy! When it comes time to naming, as much as possible :))
- E