Bug 2628 – obj[n] not allowed for user-defined tuples

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2009-01-27T23:41:00Z
Last change time
2015-06-09T05:15:22Z
Assigned to
nobody
Creator
andrei

Comments

Comment #0 by andrei — 2009-01-27T23:41:30Z
This is an annoying issue that makes tuples one order of magnitude less appealing than they might be. Currently, there is no literal to define type tuples but the TypeTuple supplants it: template TypeTuple(T...) { alias T TypeTuple; } Given: alias TypeTuple!(int, float) T; you can use its type members with T[0] and T[1]. Furthermore, creating a value of type T also works and allows you to access the members with t[0] and t[1]. There are some problems with tuple values, e.g. they can't be returned from functions: T fun() { T result; result[0] = 1; result[1] = 1.1; return result; // can't return a tuple } So then I created std.typecons.Tuple which has a field of type TypeTuple and various ways to get at it. The problem with that is that I can't reproduce the most desirable for getting fields: var[index]. What can be done is: (1) var.field[0], var.field[1] This is bad because these can't be functions; field must be exposed directly. Therefore, writing tuple proxies that use the same syntax is impossible. That turns out to be a major problem. (2) var.at!(0), var.at!(1) This is ugly and requires one to press shift a million times. The alternative var.at!0 works, but doesn't quite cut the mustard. (3) var._0, var._1 This is ugly too, and also needs to be complemented with one of (1) or (2) because it doesn't allow iteration with numbers. Never grew on me, and I've been using it for quite a while. So we'd want to allow var[0], var[1] etc. There are several possibilities of doing so, the simplest being CTFE: struct Tuple(T...) { private T field; ref T[i] opIndex(size_t i) { return field[i]; } } If you pass a compile-time index, the function should work. Right now it does only confuse the compiler :o).
Comment #1 by samukha — 2009-01-28T02:25:11Z
> struct Tuple(T...) > { > private T field; > ref T[i] opIndex(size_t i) { return field[i]; } > } That won't work as the return type of the function would depend on the function argument. At least until the requirement for CTFE functions to be evaluatable both at compile time and at run time is abolished.
Comment #2 by andrei — 2009-01-28T08:09:09Z
*** Bug 2629 has been marked as a duplicate of this bug. ***
Comment #3 by andrei — 2009-01-28T08:35:51Z
(In reply to comment #1) > > struct Tuple(T...) > > { > > private T field; > > ref T[i] opIndex(size_t i) { return field[i]; } > > } > > That won't work as the return type of the function would depend on the function > argument. At least until the requirement for CTFE functions to be evaluatable > both at compile time and at run time is abolished. Indeed. I also discussed that with Walter last night and allowing it is possible but would have ripples through the rest of the compiler. The feature can be allowed; the problem is that there's no obvious way to allow it without making it a hack that people will need to learn.
Comment #4 by kamm-removethis — 2009-01-28T10:31:41Z
What about allowing a template signature for opIndex T opIndex(size_t i)(); that is only valid if there's no classical opIndex defined. For types with such an opIndex, rewrite instance[j] as instance.opIndex!(j)() for compile-time constant indices. opSlice could get the same treatment. While it'd still be a special case, it'd be one that could just be added to the list of operator overloads without confusing people unduly.
Comment #5 by andrei — 2011-01-08T15:55:08Z
Fixed a while ago.