Thoughts on safe GC-less slices

Paul Backus snarwin at gmail.com
Wed Nov 17 03:45:03 UTC 2021


On Wednesday, 17 November 2021 at 02:08:44 UTC, Ben Jones wrote:
> On Wednesday, 17 November 2021 at 01:33:46 UTC, Paul Backus 
> wrote:
>> This is basically the same lowering already provided by 
>> [`opApply`][1]. The only differences are
>>
>> - `opApply` returns an integer code (used for control flow) 
>> rather than the return value of the callback.
>> - `opApply` uses the `foreach` keyword instead of `let`.
>>
>> [1]: https://dlang.org/spec/statement.html#foreach-statement
>
> Good point, although `let` would allow you to do arbitrary 
> indexing and sub-slicing with the borrowed as opposed to 
> looping through the whole slice in order.

You misunderstand. All the compiler does when you implement 
`opApply` is transform code that looks like this:

```d
foreach (a; b) { /* stuff */ }
```

...into code that looks like this:

```d
b.opApply((a) { /* stuff */ })
```

It is entirely possible to write an `opApply` method that does 
not do any looping, and only calls its delegate argument 
once--just like your proposed `opLet` would.

Of course, you wouldn't normally do this, because it would be 
very surprising to use the `foreach` keyword for something other 
than iteration. But there is no *technical* reason you couldn't.


More information about the Digitalmars-d mailing list