www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Swift deprecate i++ and c-style for loop

reply Suliman <evermind live.ru> writes:
https://github.com/apple/swift/blob/master/CHANGELOG.md

func slices() {
     var array = ["First", "Second", "Third", "Fourth"]
     array.removeLast()
     array.removeFirst()
}

also look very intuitive. I looked at std.algorithm.mutation and 
did not find anything for this. I see only simple remove option.
Feb 24 2016
next sibling parent reply Meta <jared771 gmail.com> writes:
On Wednesday, 24 February 2016 at 16:37:04 UTC, Suliman wrote:
 https://github.com/apple/swift/blob/master/CHANGELOG.md

 func slices() {
     var array = ["First", "Second", "Third", "Fourth"]
     array.removeLast()
     array.removeFirst()
 }

 also look very intuitive. I looked at std.algorithm.mutation 
 and did not find anything for this. I see only simple remove 
 option.
The D equivalent is this: array = array[0..$ - 1]; array = array[1..$]; Arguably just as intuitive.
Feb 24 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/24/16 11:40 AM, Meta wrote:
 On Wednesday, 24 February 2016 at 16:37:04 UTC, Suliman wrote:
 https://github.com/apple/swift/blob/master/CHANGELOG.md

 func slices() {
     var array = ["First", "Second", "Third", "Fourth"]
     array.removeLast()
     array.removeFirst()
 }

 also look very intuitive. I looked at std.algorithm.mutation and did
 not find anything for this. I see only simple remove option.
The D equivalent is this: array = array[0..$ - 1]; array = array[1..$]; Arguably just as intuitive.
I think there is a difference -- Swift arrays are not slices, so Array is not the same type as ArraySlice. I believe Array.removeFirst is O(n). I think it also returns the element removed. I also believe that it will affect any slices of the array, unlike D code. Swift also has Array.dropFirst(n), which does what the D slice does (and returns an ArraySlice). Arrays also support slicing like D does, though with different index range notation. Not 100% sure, the docs are not super clear on this. I'm quite glad D stuck with the same type for arrays and array slices. -Steve
Feb 24 2016
parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Wednesday, 24 February 2016 at 18:06:19 UTC, Steven 
Schveighoffer wrote:
 I'm quite glad D stuck with the same type for arrays and array 
 slices.
And how will you get around this when not having a GC?
Feb 24 2016
next sibling parent reply rsw0x <anonymous anonymous.com> writes:
On Wednesday, 24 February 2016 at 20:47:50 UTC, Ola Fosheim 
Grøstad wrote:
 On Wednesday, 24 February 2016 at 18:06:19 UTC, Steven 
 Schveighoffer wrote:
 I'm quite glad D stuck with the same type for arrays and array 
 slices.
And how will you get around this when not having a GC?
Could you expand upon what you mean here? I have an entire D project not using the GC and make use of slices everywhere.
Feb 24 2016
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, 24 February 2016 at 21:03:12 UTC, rsw0x wrote:
 On Wednesday, 24 February 2016 at 20:47:50 UTC, Ola Fosheim 
 Grøstad wrote:
 On Wednesday, 24 February 2016 at 18:06:19 UTC, Steven 
 Schveighoffer wrote:
 I'm quite glad D stuck with the same type for arrays and 
 array slices.
And how will you get around this when not having a GC?
Could you expand upon what you mean here? I have an entire D project not using the GC and make use of slices everywhere.
Slicing non-GC memory so that you get dynamic arrays is fine, but then you have to have somewhere else managing that memory, whereas you don't really have to worry about it when using the GC, and you have to avoid array operations which would use the GC (appending or concatenation). So, having code that doesn't use the GC but does use dynamic array treat dynamic arrays the same way that a program that uses the GC would treat them can become problematic - particularly since it's trivial to have dynamic arrays floating around referring to some chunk of malloced memory without realizing that they're still around when you free that malloced memory. So, yes. D's dynamic arrays work without the GC (barring the few operations that require the GC), but they're designed with the GC in mind and don't manage their own memory, meaning that you almost certainly need to treat them a bit differently when you don't have the GC (certainly, you have to be a lot more careful about what code you let hold onto dynamic arrays in order to avoid having them exist after the memory they refer to has been freed). But doing stuff like feeding dynamic arrays which are slices of malloced memory to a bunch of functions that just process them and give you a result without holding onto them or attempting to append to them should work just fine. It's the stuff that might hold onto them where things get hairy. - Jonathan M Davis
Feb 24 2016
prev sibling parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Wednesday, 24 February 2016 at 21:03:12 UTC, rsw0x wrote:
 Could you expand upon what you mean here? I have an entire D 
 project not using the GC and make use of slices everywhere.
Then you need ownership of arrays, not just slices. You can of course use slices to represent non-ownership. In C++ you could use std::vector + gsl::span. Just using gsl::span or D's slices everywhere is a very bad idea.
Feb 24 2016
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/24/16 3:47 PM, Ola Fosheim Grøstad wrote:
 On Wednesday, 24 February 2016 at 18:06:19 UTC, Steven Schveighoffer wrote:
 I'm quite glad D stuck with the same type for arrays and array slices.
And how will you get around this when not having a GC?
I don't follow. D's arrays will always have a GC. If you have a different type of array, it's a different type, it can do whatever it wants. -Steve
Feb 25 2016
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, 25 February 2016 at 19:03:12 UTC, Steven 
Schveighoffer wrote:
 On 2/24/16 3:47 PM, Ola Fosheim Grøstad wrote:
 On Wednesday, 24 February 2016 at 18:06:19 UTC, Steven 
 Schveighoffer wrote:
 I'm quite glad D stuck with the same type for arrays and 
 array slices.
And how will you get around this when not having a GC?
I don't follow. D's arrays will always have a GC. If you have a different type of array, it's a different type, it can do whatever it wants.
Remember that a dynamic array does not necessarily refer to GC-allocated memory. The dynamic array operations don't care about that at all, but it does make it so that you can have a problem figuring out when it's okay to free the memory that you sliced them from if you start passing them around all over the place and potentially storing them. So, in that regard, the fact that a dynamic array doesn't really own or manage its own memory can complicate things when the dynamic array was not allocated via the GC (be it because it was sliced from malloced memory or sliced from a static array or whatever). There's also the issue that some folks disable the GC, which doesn't prevent dynamic arrays from working aside from the few operations that require the GC, but it does mean that you to be that much more careful about how you use them. Personally, I think that using malloc or a static array to back a dynamic array works great if you're dealing with localized stuff that you're going to pass to pure, free functions (which therefore can't slice the dynamic array and store it elsewhere), but once you're dealing with dynamic arrays that get passed around and stored arbitrarily, it gets seriously iffy to slice malloced memory or static arrays and start passing those dynamic arrays around. It quickly becomes far more sensible to create a container that gives you range rather than deal with dynamic arrays. - Jonathan M Davis
Feb 25 2016
prev sibling parent David Nadlinger <code klickverbot.at> writes:
On Wednesday, 24 February 2016 at 16:37:04 UTC, Suliman wrote:
 func slices() {
     var array = ["First", "Second", "Third", "Fourth"]
     array.removeLast()
     array.removeFirst()
 }

 also look very intuitive.
Have a look at http://dlang.org/phobos/std_range.html#dropOne. — David
Feb 24 2016