October 02, 2012
Jonathan M Davis wrote:
> if length can be specifically ulong and the type is random access, then its
> indices will need to be ulong), so unfortunately, the situation is not so
> simple that you can always assume size_t (even you should arguably be able
> to).

It seems that isRandomAccessRange doesn't check that opIndex parameter type and length() return type are the same. Do you think it should?

October 02, 2012
On Tuesday, 2 October 2012 at 16:59:38 UTC, Jonathan M Davis wrote:
> On Tuesday, October 02, 2012 18:45:50 Peter Alexander wrote:
>> On Tuesday, 2 October 2012 at 16:29:28 UTC, Simen Kjaeraas wrote:
>> > On 2012-10-02, 18:09, Peter Alexander wrote:
>> >> On Tuesday, 2 October 2012 at 13:17:45 UTC, monarch_dodra
>> >> 
>> >> wrote:
>> >>> If you've ever worked on a template that needs to index a
>> >>> range, you may have run into this problem: What is the type
>> >>> you should use to index an RA range?
>> >> 
>> >> Forgive my ignorance. What's wrong with size_t?
>> > 
>> > That not all ranges use it? If the range uses int, short, byte
>> > (I wonder why they'd do it, though), using size_t will not even
>> > compile.
>> 
>> That's kind of my point. Unless there's a compelling reason not
>> to, I'd suggest we standardise on size_t indexing (and length)
>> and avoid this issue altogether.
>> 
>> C++ containers have a size_type typedef. No one uses it.
>> 
>> Let's keep things simple instead of complicating things for the
>> sake of unwanted "flexibility".
>
> In general, all ranges _should_ use size_t for both length and indexing, but
> for a few range types in Phobos specifically use ulong (e.g. IIRC iota does in
> order to work properly with ranges or long and ulong on 32-bit systems). I see
> _zero_ reason to support lengths or indices smaller than size_t. Types that do
> that are badly designed IMHO. But we already have a precedent that you can't
> always assume size_t (at least for length - I'm not sure about indices - but
> if length can be specifically ulong and the type is random access, then its
> indices will need to be ulong), so unfortunately, the situation is not so
> simple that you can always assume size_t (even you should arguably be able
> to).
>
> - Jonathan M Davis

Given your stance of "I see _zero_ reason to support lengths or indices smaller than size_t" and "Types that do that are badly designed IMHO":

Are you agreeing with my proposed type tightening? If anything, it weeds out the "bad designs" for which you had no wish to support, while allowing better support for those that do.

October 02, 2012
On Tuesday, October 02, 2012 15:17:58 monarch_dodra wrote:
> You might think "just use typeof(length)" BUT:
> *you aren't even guaranteed that "typeof(length)" will be
> correct! Certain ranges, such as iota, will return a length
> usually of type uint, but be indexed with ulong... :/
> *Infinite ranges don't have length...

I'd argue that that's a bug in iota. iota's length even specifically returns _IndexType_.

It makes no sense for length, opIndex, or opSlice to vary in type at all. They should all use the same type (ideally size_t). The fact that it's not outright required to be size_t is bad enough (though IIRC iota had some good reasons for using ulong).

> These are not big changes I'm proposing, but they *may* break some existing ranges. Those ranges are arguably retarded, and these changes would enforce correctness, but they'd break none the less. I'd like some feedback if you think this trait is worth pushing?

Requiring that length, opIndex, and opSlice all use the same index type would be very much the right way to go IMHO. If that's done however, I don't know if we'll really need IndexType (though it may still be a good idea to add it).

In addition, I'd argue that they should require that they all be at least as large as size_t (ideally, they'd even have to be either size_t or ulong and that's it - no signed types allowed), but that may be too strict at this point given that it could break existing code that did stupid stuff like use int (which _way_ too many people seem inclined to do).

- Jonathan M Davis
October 02, 2012
On Tuesday, October 02, 2012 19:10:53 monarch_dodra wrote:
> Given your stance of "I see _zero_ reason to support lengths or indices smaller than size_t" and "Types that do that are badly designed IMHO":
> 
> Are you agreeing with my proposed type tightening? If anything, it weeds out the "bad designs" for which you had no wish to support, while allowing better support for those that do.

Ideally, only size_t would be allowed. Reality makes it so that we need ulong in some cases (e.g. iota). Given that fact, you'd ideally restrict it to size_t or ulong specfically (or at least IndexType.sizeof >= size_t.sizeof). The problem is that I'm quite sure that there are plenty of programmers out there who have been using int for length and indices even though it's a horribly bad idea. It's a classic mistake. So, while requiring size_t or ulong would be great, I'd be very surprised if it didn't break a fair bit of code out there. Given that fact that and Andrei's increased resistance to potential code breakage, I don't know that we can make that change.

Still, I'd try to push for it though. It's bad enough that length and indices are allowed to be something other than size_t at all, but anything smaller than size_t (including using int specifically) _will_ cause problems for those who do that, if nothing else because size_t is ulong on 64-bit systems and using int will therefore mean that code using int for length will likely break when compiled on 64-bit systems (particularly when interacting with arrays). That's probably even a good argument for why we could restrict length and indices to size_t or greater even if it might break code (since it'll generally break when compiled on 64-bit systems anyway). This sort of change is going to have to get passed Andrei though, so we'll need his buy-in no matter what we do.

- Jonathan M Davis
October 02, 2012
On 10/2/12 12:45 PM, Peter Alexander wrote:
> On Tuesday, 2 October 2012 at 16:29:28 UTC, Simen Kjaeraas wrote:
>> On 2012-10-02, 18:09, Peter Alexander wrote:
>>
>>> On Tuesday, 2 October 2012 at 13:17:45 UTC, monarch_dodra wrote:
>>>> If you've ever worked on a template that needs to index a range, you
>>>> may have run into this problem: What is the type you should use to
>>>> index an RA range?
>>>
>>> Forgive my ignorance. What's wrong with size_t?
>>
>> That not all ranges use it? If the range uses int, short, byte
>> (I wonder why they'd do it, though), using size_t will not even
>> compile.
>
> That's kind of my point. Unless there's a compelling reason not to, I'd
> suggest we standardise on size_t indexing (and length) and avoid this
> issue altogether.

Yes. Unfortunately there are few, few cases in which size_t is insufficient (e.g. an input range from a file or a large iota, both on 32-bit builds). I personally think these are too few to need formal support.

> C++ containers have a size_type typedef. No one uses it.

Agreed.

> Let's keep things simple instead of complicating things for the sake of
> unwanted "flexibility".

Yes. We should curb some corner cases of current range design in the direction of simplifying things.


Andrei
October 02, 2012
On 10/2/12 1:07 PM, monarch_dodra wrote:
> I don't know, forcing an implementer on size_t is pretty gratuitous.
> Why can't he be free to choose his own index type?

Too much freedom can be detrimental (as is in this case).

Andrei
October 02, 2012
On Tuesday, 2 October 2012 at 17:13:48 UTC, Jonathan M Davis wrote:
> On Tuesday, October 02, 2012 15:17:58 monarch_dodra wrote:
>> You might think "just use typeof(length)" BUT:
>> *you aren't even guaranteed that "typeof(length)" will be
>> correct! Certain ranges, such as iota, will return a length
>> usually of type uint, but be indexed with ulong... :/
>> *Infinite ranges don't have length...
>
> I'd argue that that's a bug in iota. iota's length even specifically returns
> _IndexType_.
>
> It makes no sense for length, opIndex, or opSlice to vary in type at all. They
> should all use the same type (ideally size_t). The fact that it's not outright
> required to be size_t is bad enough (though IIRC iota had some good reasons
> for using ulong).

To be honest, I think I may have put too much stress on the "details". I agree we may want to enforce they have matching types (or at least, a smart hierarchy). That wasn't the root if the reason for IndexType.

The "big picture issue" here is writing wrapper ranges, such as "AssumeSorted". Or "take", or every other sweet-ass range adaptors we have in std.range. If "take" doesn't know how to index the sub-range, how can it properly work with ranges that always use ulong, AND at the same time, support that ranges that always use size_t (uint on x86)? Answer: It CAN'T.

CAN'T CAN'T CAN'T.

Keep in mind, infinite ranges don't have length, so that's out of the equation...

>> These are not big changes I'm proposing, but they *may* break
>> some existing ranges. Those ranges are arguably retarded, and
>> these changes would enforce correctness, but they'd break none
>> the less. I'd like some feedback if you think this trait is worth
>> pushing?
>
> Requiring that length, opIndex, and opSlice all use the same index type would
> be very much the right way to go IMHO. If that's done however, I don't know if
> we'll really need IndexType (though it may still be a good idea to add it).
>
> In addition, I'd argue that they should require that they all be at least as
> large as size_t (ideally, they'd even have to be either size_t or ulong and
> that's it - no signed types allowed), but that may be too strict at this point
> given that it could break existing code that did stupid stuff like use int
> (which _way_ too many people seem inclined to do).
>
> - Jonathan M Davis

You'd still need IndexType for the reasons mentioned above, unless you wanted to write "auto opIndex(ParameterTypeTuple(R.opIndex)[1] n)" in all your ranges. AND, you'd require the array specialization (which would default to size_t).

The actual support of things smaller than size_t, at that point, would become a non-issue. Just:

//----
static if (isRandomAccessRange!R)
    auto opIndex(IndexType!R n)
    {
        return r[n];
    }
//----

Clean, concise. Supports both size_t and ulong (and others).
October 02, 2012
On Tuesday, 2 October 2012 at 17:07:19 UTC, monarch_dodra wrote:
> [SNIP]

You know what, I think I have a better. Idea. All of this came up because I've had iota break my compiles WAY more often then I'd have liked. But I think I know of another solution.

I think it would be nice if we enforced that all ranges used size_t. Everywhere. And it was enforced.

I'm sorry, I like extremes.
October 02, 2012
On Tuesday, October 02, 2012 19:08:59 Piotr Szturmaj wrote:
> Jonathan M Davis wrote:
> > if length can be specifically ulong and the type is random access, then
> > its
> > indices will need to be ulong), so unfortunately, the situation is not so
> > simple that you can always assume size_t (even you should arguably be able
> > to).
> 
> It seems that isRandomAccessRange doesn't check that opIndex parameter type and length() return type are the same. Do you think it should?

Definitely. It makes no sense to be able to have a length greater than you can index (beyond the fact that the last index is length - 1), and it makes no sense to be able to index anything greater than length as far as the size of types go.

- Jonathan M Davis
October 02, 2012
On Tuesday, October 02, 2012 19:37:18 monarch_dodra wrote:
> On Tuesday, 2 October 2012 at 17:07:19 UTC, monarch_dodra wrote:
> > [SNIP]
> 
> You know what, I think I have a better. Idea. All of this came up because I've had iota break my compiles WAY more often then I'd have liked. But I think I know of another solution.
> 
> I think it would be nice if we enforced that all ranges used size_t. Everywhere. And it was enforced.
> 
> I'm sorry, I like extremes.

Personally, I'd love that. The problem is that iota was specifically changed to use ulong to support handling long and ulong properly on 32-bit systems. Without it, you can't actually use long or ulong with a step of 1 beyond uint.max (at least, I _think_ that that was the issue). Requiring that the length and indices be size_t undermines that.

Now, I have no idea how much of a problem that realistically is. After all, you can't have an array of length > uint.max or 32-bit systems, so restricting iota to a length of uint.max isn't necessarily all than unreasonable IMHO. And per that argument, we _could_ change iota to use size_t again and just outright require that length and indices be size_t.

- Jonathan M Davis
1 2 3
Top | Discussion index | About this forum | D home