4

Is there any way to refer to the types of a tuple's fields without recapitulation? Something along the lines of this:

pub struct Foo(i16, u64);
impl Foo {
    pub fn get_bar(&self) -> Self::0 { self.0 }
    pub fn get_baz(&self) -> Self::1 { self.1 }
}

fn main() {
    let foo = Foo(123, 456);
    println!("bar: {}", foo.get_bar());
    println!("baz: {}", foo.get_baz());
}

(This does not work, because Self::0 isn't a thing. But maybe it is and I just don't know what it's called.)

Reid Rankin
  • 1,078
  • 8
  • 26
  • Something like C++'s `decltype` (and older `typeof` extensions)? No, I don't think so. I expect there are old RFCs but there seems to be no such feature even in the unstable intrinsics. – Masklinn Apr 10 '20 at 06:02
  • 1
    what is the point if you need getter on a tuple it's should be a structure. – Stargateur Apr 10 '20 at 08:25
  • @Stargateur It's just an example. I didn't want to invent functionality just to justify the problem. Pretend I'm doing something useful in there. – Reid Rankin Apr 10 '20 at 08:27
  • I don't want to, if you can't found a good use case it's a big clue that it's isn't useful. Note that I said that on a meta view not about your specific question. – Stargateur Apr 10 '20 at 08:33
  • 2
    @Stargateur Not everything is an XY problem. There's a perfectly good use case which I don't want to get bogged down in and/or can't go into. I don't ask these questions if I haven't considered the issue thoroughly, and, in general, if I'm asking for pointers on my yak shaving technique it's because I'm actually interested in getting a well-shaved yak. – Reid Rankin Apr 10 '20 at 08:51
  • 2
    Yes but consider that 1 Why should your point of view be valid ? 2. We are not here to just answer question, sharing is expecting from user, you don't want to share your usercase to keep it secret ? So why should we help you if you don't help anyone with your question ? – Stargateur Apr 10 '20 at 09:01
  • 1
    @Stargateur As I've said, the particular usecase is irrelevant to the issue at hand: whether Rust has a way to refer to the types of tuple fields. That's my question, not whether doing so is a good idea in the context of a particular set of goals. Keeping the discussion limited to the particular issue I asked about will help others who come searching for the same answer -- especially because they probably won't be doing specifically what I'm doing. Not coupling my question to a particular use case actually *improves* the quality and usefulness of both the question itself and its answers. – Reid Rankin Apr 10 '20 at 09:16
  • @Masklinn Sucks, but what are you gonna do? Can you go ahead and put that in an answer? – Reid Rankin Apr 10 '20 at 09:18
  • " Not coupling my question to a particular use case actually improves the quality and usefulness of both the question itself and its answers." quoting myself: "Note that I said that on a meta view not about your specific question." – Stargateur Apr 10 '20 at 09:44
  • 1
    @ReidRankin This kind of behavior is also limited for structs but i don't get it what you are really trying to aviod in here, what you are trying to achieve is simply provided with generics : https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3c74fe376e84075537e645f1c8f729f6, at least we need a detail like _i cannot modify the tuple etc.._. – Ömer Erden Apr 10 '20 at 14:10
  • @ÖmerErden The problem there is that I have to change the type signature to do that, and I can't. And I appreciate your efforts, but Masklinn answered my question in a succinct and timely manner in the first comment -- I'm just waiting for him to put it in answer form so I can accept it. – Reid Rankin Apr 11 '20 at 07:35

0 Answers0