October 04
On Friday, 4 October 2019 at 19:03:14 UTC, Dennis wrote:
> You're right, I'm confused. I recall there was a situation where you had to explicitly slice a static array, but I can't think of it now.

When passing to a range template it is necessary, otherwise the template will see it as non-resizable and it will fail the range constraint check.

(personally though I like to explicitly slice it all the time though, it is more clear and the habit is nice)
October 04
On Fri, Oct 04, 2019 at 07:08:04PM +0000, Adam D. Ruppe via Digitalmars-d-learn wrote:
> On Friday, 4 October 2019 at 19:03:14 UTC, Dennis wrote:
> > You're right, I'm confused. I recall there was a situation where you had to explicitly slice a static array, but I can't think of it now.
> 
> When passing to a range template it is necessary, otherwise the template will see it as non-resizable and it will fail the range constraint check.
> 
> (personally though I like to explicitly slice it all the time though, it is more clear and the habit is nice)

Yeah, and it's always better to consciously slice it, and therefore be reminded to think about the implications of slicing it, so that you'll be aware not to let the slice leak past the lifetime of the underlying static array.


T

-- 
Unix was not designed to stop people from doing stupid things, because that would also stop them from doing clever things. -- Doug Gwyn
October 04
On Friday, 4 October 2019 at 18:53:30 UTC, H. S. Teoh wrote:
> Here's an actual working example that illustrates the pitfall of this implicit conversion:

Luckily it's caught by -dip1000
October 04
On Friday, 4 October 2019 at 19:08:04 UTC, Adam D. Ruppe wrote:
> (personally though I like to explicitly slice it all the time though, it is more clear and the habit is nice)

Turns out I have this habit as well. I'm looking through some of my code and see redundant slicing everywhere.
October 04
On Fri, Oct 04, 2019 at 07:21:34PM +0000, Dennis via Digitalmars-d-learn wrote:
> On Friday, 4 October 2019 at 18:53:30 UTC, H. S. Teoh wrote:
> > Here's an actual working example that illustrates the pitfall of this implicit conversion:
> 
> Luckily it's caught by -dip1000

Nice!


T

-- 
"A man's wife has more power over him than the state has." -- Ralph Emerson
October 04
On Friday, October 4, 2019 1:22:26 PM MDT Dennis via Digitalmars-d-learn wrote:
> On Friday, 4 October 2019 at 19:08:04 UTC, Adam D. Ruppe wrote:
> > (personally though I like to explicitly slice it all the time though, it is more clear and the habit is nice)
>
> Turns out I have this habit as well. I'm looking through some of my code and see redundant slicing everywhere.

Really, it should be required by the language, because it's not something that you want to be hidden. It's an easy source of bugs - especially once you start passing that dynamic array around. It's incredibly useful to be able to do it, but you need to be careful with such code. It's the array equivalent of taking the address of a local variable and passing a pointer to it around.

IIRC, -dip1000 improves the situation by making it so that the type of a slice of a static array is scope, but it's still easy to miss, since it only affects @safe code. It should certainly be possible to slice a static array in @system code without having to deal with scope, but the fact that explicit slicing isn't required in such a case makes it more error-prone than it would be if explicit slicing were required.

- Jonathan M Davis



Next ›   Last »
1 2