Re: Returning a reference to be manipulated

2023-04-16 Thread Dennis via Digitalmars-d-learn
On Saturday, 15 April 2023 at 21:00:01 UTC, kdevel wrote: On Saturday, 15 April 2023 at 15:50:18 UTC, Dennis wrote: [...] care about the type / mutability of the pointer. Returning `i`'s address in a long does not trigger the escape detector: It doesn't care about the type of pointer, but

Re: Returning a reference to be manipulated

2023-04-15 Thread kdevel via Digitalmars-d-learn
On Saturday, 15 April 2023 at 15:50:18 UTC, Dennis wrote: [...] care about the type / mutability of the pointer. Returning `i`'s address in a long does not trigger the escape detector: ``` long foo (long s, return ref int i) { s = cast (long) return s; } auto bar () { int i;

Re: Returning a reference to be manipulated

2023-04-15 Thread Dennis via Digitalmars-d-learn
On Saturday, 15 April 2023 at 14:33:52 UTC, kdevel wrote: Does that make sense? Whether it makes sense is subjective, but it is by design. Escape analysis considers every pointer the same, it doesn't care about the type / mutability of the pointer. In `@system` / `@trusted` code, you could

Re: Returning a reference to be manipulated

2023-04-15 Thread kdevel via Digitalmars-d-learn
On Saturday, 15 April 2023 at 14:10:57 UTC, Dennis wrote: [...] This adds complexity, just to add some 'intermediate' safety between `@system` and `@safe` in a few cases. It's better to keep the rules simple and consistent. Thanks for the answer! While playing around with return ref I came

Re: Returning a reference to be manipulated

2023-04-15 Thread Dennis via Digitalmars-d-learn
On Saturday, 15 April 2023 at 14:10:57 UTC, Dennis wrote: This adds complexity, just to add some 'intermediate' safety between `@system` and `@safe` in a few cases. It's better to keep the rules simple and consistent. To quote my past self: There used to be different rules for lifetime

Re: Returning a reference to be manipulated

2023-04-15 Thread Dennis via Digitalmars-d-learn
On Saturday, 15 April 2023 at 13:20:09 UTC, kdevel wrote: Under which circumstances is it a mistake to insert the `return` at the indicated position? If there are none why can't it be done implicitly (automatically)? It could be done in the easy example you posted, but generalizing it is

Re: Returning a reference to be manipulated

2023-04-15 Thread kdevel via Digitalmars-d-learn
On Saturday, 15 April 2023 at 13:24:52 UTC, Richard (Rikki) Andrew Cattermole wrote: On 16/04/2023 1:20 AM, kdevel wrote: On Saturday, 15 April 2023 at 12:45:31 UTC, Richard (Rikki) Andrew Cattermole wrote: It was bad analysis by the compiler, which has since been corrected. It should have

Re: Returning a reference to be manipulated

2023-04-15 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
On 16/04/2023 1:20 AM, kdevel wrote: On Saturday, 15 April 2023 at 12:45:31 UTC, Richard (Rikki) Andrew Cattermole wrote: It was bad analysis by the compiler, which has since been corrected. It should have been applied only to @safe code. But why is the @unsafe programmer now left

Re: Returning a reference to be manipulated

2023-04-15 Thread kdevel via Digitalmars-d-learn
On Saturday, 15 April 2023 at 12:45:31 UTC, Richard (Rikki) Andrew Cattermole wrote: It was bad analysis by the compiler, which has since been corrected. It should have been applied only to @safe code. But why is the @unsafe programmer now left unprotected in cases like this ``` ref int

Re: Returning a reference to be manipulated

2023-04-15 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
It was bad analysis by the compiler, which has since been corrected. It should have been applied only to @safe code.

Re: Returning a reference to be manipulated

2023-04-15 Thread kdevel via Digitalmars-d-learn
On Friday, 14 April 2023 at 11:18:21 UTC, Dennis wrote: On Friday, 14 April 2023 at 10:31:58 UTC, kdevel wrote: But in fact it is returned unless it is `return ref`. When using `return ref`, `return scope`, `scope` etc., you should be using the latest compiler and annotate functions you

Re: Returning a reference to be manipulated

2023-04-14 Thread Dennis via Digitalmars-d-learn
On Friday, 14 April 2023 at 10:31:58 UTC, kdevel wrote: But in fact it is returned unless it is `return ref`. When using `return ref`, `return scope`, `scope` etc., you should be using the latest compiler and annotate functions you want checked with `@safe`. In previous versions, the

Re: Returning a reference to be manipulated

2023-04-14 Thread kdevel via Digitalmars-d-learn
On Friday, 14 April 2023 at 09:42:14 UTC, Bastiaan Veelo wrote: [...] Up to dmd v2.100.2 I am warned/get an error during compilation: ``` $ dmd returnref2.d returnref2.d(3): Deprecation: returning `i` escapes a reference to parameter `i` returnref2.d(1):perhaps annotate the parameter

Re: Returning a reference to be manipulated

2023-04-14 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 14 April 2023 at 00:50:31 UTC, kdevel wrote: ``` ref int foo (ref int i) { return i; } ref int bar () { int i; return foo (i); } void main () { import std.stdio; auto i = bar; i.writeln; } ``` Up to dmd v2.100.2 I am warned/get an error during compilation: ``` $

Re: Returning a reference to be manipulated

2023-04-13 Thread kdevel via Digitalmars-d-learn
On Thursday, 13 April 2023 at 22:09:48 UTC, Jacob Shtokolov wrote: [...] ref opIndex(string key) return [...] Regarding the return ref I found this code of 2019 on my harddisk which is from or was motivated by a dconf talk: ``` ref int foo (ref int i) { return i; } ref int bar () {

Re: Returning a reference to be manipulated

2023-04-13 Thread Jacob Shtokolov via Digitalmars-d-learn
On Thursday, 13 April 2023 at 07:05:10 UTC, Chris Katko wrote: I'm trying to figure out how to return a reference to something that may not be a reference type. ```d @safe: struct Stats { float[string] data; ref opIndex(string key) return { // The `require()` takes care

Re: Returning a reference to be manipulated

2023-04-13 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 13 April 2023 at 07:05:10 UTC, Chris Katko wrote: Right now, I'm using pointers which resolves to: ```D // float* opIndex(string key){...} using pointer (*s["tacos"])++; // works with pointer, but is strange looking s["tacos"]++; // preferred syntax or something similar ``` You

Re: Returning a reference to be manipulated

2023-04-13 Thread IchorDev via Digitalmars-d-learn
On Thursday, 13 April 2023 at 07:05:10 UTC, Chris Katko wrote: I'm trying to figure out how to return a reference to something that may not be a reference type. ```D struct stats { float[string] data=0; float ref opIndex(string key) { return data[key]; // want a ref to a specific element

Returning a reference to be manipulated

2023-04-13 Thread Chris Katko via Digitalmars-d-learn
I'm trying to figure out how to return a reference to something that may not be a reference type. ```D struct stats { float[string] data=0; float ref opIndex(string key) { return data[key]; // want a ref to a specific element } } void test() { stats foo; auto x = foo.bar(); // returns