Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasons for Dot Notation for Tuple

Tags:

rust

Is there any technical reason Rust is designed to use dot notation for tuples instead of using index notation (t[2])?

let t = (20u32, true, 'b')
t.2 // -> 'b'

Dot notation seems natural in accessing struct's and object's properties. I couldn't find a resource or explanation online.

like image 506
Pandemonium Avatar asked Aug 16 '15 00:08

Pandemonium


People also ask

What is the purpose of dot notation?

What is the Dot Notation? In simple words, the dot (.) notation is a way to access the attribute and methods of each method of instances of different object classes. It is usually preceded by the object instance while the right end of the dot notation contains the attributes and methods.

Why is dot notation used Python?

Dot notation indicates that you're accessing data or behaviors for a particular object type. When you use dot notation, you indicate to Python that you want to either run a particular operation on, or to access a particular property of, an object type.

Why dot is used in JavaScript?

The dot notation and bracket notation both are used to access the object properties in JavaScript. The dot notation is used mostly as it is easier to read and comprehend and also less verbose.

What is Dot in coding?

(dot) operator is used to access class, structure, or union members. The member is specified by a postfix expression, followed by a . (dot) operator, followed by a possibly qualified identifier or a pseudo-destructor name. (A pseudo-destructor is a destructor of a nonclass type.)


1 Answers

I had no part in the design decisions, but here's my perspective:

Tuples contain mixed types. That is, the property type_of(t[i]) == type_of(t[j]) cannot be guaranteed.

However, conventional indexing works on the premise that the i in t[i] need not be a compile-time constant, which in turn means that the type of t[i] needs to be uniform for all possible i. This is true in all other rust collections that implement indexing. Specifically, rust types are made indexable through implementing the Index trait, defined as below:

pub trait Index<Idx> where Idx: ?Sized {
    type Output: ?Sized;
    fn index(&'a self, index: Idx) -> &'a Self::Output;
}

So if you wanted a tuple to implement indexing, what type should Self::Output be? The only way to pull this off would be to make Self::Output an enum, which means that element accesses would have to be wrapped around a useless match t[i] clause (or something similar) on the programmer's side, and you'll be catching type errors at runtime instead of compile-time.

Furthermore, you now have to implement bounds-checking, which is again a runtime error, unless you're clever in your tuple implementation.

You could bypass these issues by requiring that the index by a compile-time constant, but at that point tuple item accesses are pretending to behave like a normal index operation while actually behaving inconsistently with respect to all other rust containers, and there's nothing good about that.

like image 78
Ponkadoodle Avatar answered Sep 20 '22 15:09

Ponkadoodle