Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
February 01, 2014 RE: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
In reference to this thread: http://forum.dlang.org/thread/ouyuujnzzvfkvxbfzyak@forum.dlang.org#post-ouyuujnzzvfkvxbfzyak:40forum.dlang.org Personally I think it was a mistake providing unsafe APIs by default. If I would have had it my way, I would introduce: byLine -> safe, doesn't reuse a buffer byLineBuffer -> reuses a buffer That way you get safe-by-default operations for the vast majority of users, and a speedy version for those who need it when they need it. This is similar to how the new regex APIs encode in their name exactly what they do, e.g. the new matchAll is self-describing rather than guessing whether match() has a default mode of "g" that matches all or not. It's probably too late to change byLine now. But warnings and notes in the comments have so far been unfruitful. I can't imagine many people are aware of warnings, and some warnings are so ridiculously long that it makes you question why a function was made to have so many caveats. For a classic example, read the warnings for toUTFz: http://dlang.org/phobos/std_utf.html#.toUTFz Safe and simple should be the default, leave the "if ((cast(size_t)p & 3) && *p == '\0') return str.ptr" wizardry for a separately named function that provides these speed benefits at the cost of safety. |
February 02, 2014 Re: RE: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic: > Personally I think it was a mistake providing unsafe APIs by default. If I would have had it my way, I would introduce: > > byLine -> safe, doesn't reuse a buffer > byLineBuffer -> reuses a buffer I agree. I proposed something related lot of time ago, see (the original title of this ER was "Safer stdin.byLine()"): http://d.puremagic.com/issues/show_bug.cgi?id=4474 Bye, bearophile |
February 02, 2014 Re: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 2/1/14, 3:07 PM, Andrej Mitrovic wrote:
> In reference to this thread:
> http://forum.dlang.org/thread/ouyuujnzzvfkvxbfzyak@forum.dlang.org#post-ouyuujnzzvfkvxbfzyak:40forum.dlang.org
>
>
> Personally I think it was a mistake providing unsafe APIs by default. If
> I would have had it my way, I would introduce:
>
> byLine -> safe, doesn't reuse a buffer
> byLineBuffer -> reuses a buffer
No. Too much breakage.
Andrei
|
February 02, 2014 Re: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sunday, 2 February 2014 at 01:03:25 UTC, Andrei Alexandrescu wrote:
> On 2/1/14, 3:07 PM, Andrej Mitrovic wrote:
>> In reference to this thread:
>> http://forum.dlang.org/thread/ouyuujnzzvfkvxbfzyak@forum.dlang.org#post-ouyuujnzzvfkvxbfzyak:40forum.dlang.org
>>
>>
>> Personally I think it was a mistake providing unsafe APIs by default. If
>> I would have had it my way, I would introduce:
>>
>> byLine -> safe, doesn't reuse a buffer
>> byLineBuffer -> reuses a buffer
>
> No. Too much breakage.
>
> Andrei
From the docs it appears as array() will handle the required copying.
std.array.array doc:
---
Returns a newly-allocated dynamic array consisting of a copy of the input range, static array, dynamic array, or class or struct with an opApply function r. Note that narrow strings are handled as a special case in an overload.
---
std.stdio.byLine doc:
---
Returns an input range set up to read from the file handle one line at a time.
The element type for the range will be Char[]. Range primitives may throw StdioException on I/O error.
Note:
Each front will not persist after popFront is called, so the caller must copy its contents (e.g. by calling to!string) if retention is needed.
---
|
February 02, 2014 Re: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sunday, 2 February 2014 at 01:03:25 UTC, Andrei Alexandrescu wrote:
> On 2/1/14, 3:07 PM, Andrej Mitrovic wrote:
>> In reference to this thread:
>> http://forum.dlang.org/thread/ouyuujnzzvfkvxbfzyak@forum.dlang.org#post-ouyuujnzzvfkvxbfzyak:40forum.dlang.org
>>
>>
>> Personally I think it was a mistake providing unsafe APIs by default. If
>> I would have had it my way, I would introduce:
>>
>> byLine -> safe, doesn't reuse a buffer
>> byLineBuffer -> reuses a buffer
>
> No. Too much breakage.
>
> Andrei
Agreed.
I wonder if the problem can be fixed another way:
1. Introduce a new function ("File.lines" perhaps) which is like byLine, but safe, and has an option to re-use a buffer (but isn't default).
2. After a while, remove documentation for byLine, but leave it in Phobos.
This way, newcomers will never see byLine and will get safe behaviour by default with "lines", and existing code will continue to work using the undocumented byLine.
|
February 02, 2014 Re: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sunday, 2 February 2014 at 01:03:25 UTC, Andrei Alexandrescu wrote:
>> I would have had it my way, I would introduce:
>>
>> byLine -> safe, doesn't reuse a buffer
>> byLineBuffer -> reuses a buffer
>
> No. Too much breakage.
How exactly is it breakage? The user code:
- will not stop to compile
- will not stop to link
- will still produce expected results
The only thing that can "break" is that the user code will lose performance where it actually does make an explicit copy. This can be solved by introducing a message with pragma(msg), directing users to get rid of unnecessary copying. The message could stick around for several releases.
|
Copyright © 1999-2021 by the D Language Foundation