Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Rust references (usually) Voldemort types?

Voldemort – he who must not be named – types are types whose names are impossible to write down in the source code. In Rust, closures have such types, because the compiler generates a new internal type for each closure. The only way to accept a closure as function argument is to accept a generic type (usually called F) which is bounded to be an Fn() (or similar) trait.

References in Rust always contain a lifetime parameter, even if this lifetime can usually be omitted. Lifetimes can't be named explicitly, because they represent some complex compiler-internal scope of some kind. The only way to interact with lifetimes is to use a generic parameter (usually called 'a) which stands for any lifetime (maybe bounded by another lifetime). Of course, there is 'static which can be named, but this is a special case and doesn't conflict with my arguing.

So: are Rust references Voldemort types? Or do I misunderstand the term “Voldemort type” or Rust references?

like image 984
Lukas Kalbertodt Avatar asked Oct 23 '16 13:10

Lukas Kalbertodt


2 Answers

Arguably no. Are the types of references and pointers in all languages considered Voldemort types? They hide something, but no.

We envision lifetimes as being regions of code outside the called function. Also, they're created roughly like that in rustc. Yet, I'd argue function signatures are the type definition of the lifetimes we actually see. And rustc is merely satisfying them. There is nothing more to the named lifetimes than what you see in the function definition.

like image 192
Jeff Burdges Avatar answered Nov 06 '22 12:11

Jeff Burdges


As someone without any particularly strong knowledge in the area:

I think the answer is probably: technically yes, but it's overly reductive. A bit like saying "all types are arrays of integers"; I mean, yes, but you're losing some useful semantic discrimination by doing that.

Voldemort types are usually to hide the implementation type from the user, either because it's only supposed to be a temporary, or you're not supposed to use anything but the interface described by the function. References are technically unnameable in their entirety, but it's not like it ever actually restricts you. I mean, even if you could name the specific lifetime, I don't think you could do anything meaningful with it (except possibly for slightly stricter lifetime checking within a function).

like image 20
DK. Avatar answered Nov 06 '22 12:11

DK.