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: class variable initialization

2023-04-15 Thread Ali Çehreli via Digitalmars-d-learn
On 4/15/23 09:06, NonNull wrote: >> struct Wrapper >> { >>Object x = new Object(); >>alias x this; >> } > Amazing, was this always so? At least for a long time. However, every Wrapper object will have a pointer to that single shared object. If you think that cost is unnecessary, you

Re: How to use @safe when a C library integration needed

2023-04-15 Thread Leonardo via Digitalmars-d-learn
On Friday, 14 April 2023 at 16:19:22 UTC, Paul Backus wrote: On Friday, 14 April 2023 at 14:10:41 UTC, Leonardo wrote: [...] No, there isn't. C is an unsafe language, so if you want to call C from `@safe` code, you have to do the work to make sure that each individual call is `@safe`.

Re: class variable initialization

2023-04-15 Thread NonNull via Digitalmars-d-learn
On Saturday, 15 April 2023 at 15:47:40 UTC, Steven Schveighoffer wrote: You can construct objects at compile time. If I understand your question properly: ```d struct Wrapper { Object x = new Object(); alias x this; } void foo(Object o) { assert(o !is null); } void main() {

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: class variable initialization

2023-04-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/15/23 7:05 AM, NonNull wrote: I want a way to default initialize a class variable to a default object (e.g. by wrapping it in a struct, because initialization to null cannot be changed directly). Such a default object is of course not available at compile time which seems to make this

Re: class variable initialization

2023-04-15 Thread NonNull via Digitalmars-d-learn
On Saturday, 15 April 2023 at 14:17:19 UTC, Vijay Nayar wrote: I believe if you do initialization at the class declaration level, then every instance of the class shares the same instance, e.g.: ``` class Var {} class MyClass { Var var = new Var(); } void main() { MyClass c1 = new

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: class variable initialization

2023-04-15 Thread Vijay Nayar via Digitalmars-d-learn
On Saturday, 15 April 2023 at 14:05:17 UTC, NonNull wrote: I want a way to default initialize a class variable to a default object (e.g. by wrapping it in a struct, because initialization to null cannot be changed directly). Such a default object is of course not available at compile time

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

class variable initialization

2023-04-15 Thread NonNull via Digitalmars-d-learn
I want a way to default initialize a class variable to a default object (e.g. by wrapping it in a struct, because initialization to null cannot be changed directly). Such a default object is of course not available at compile time which seems to make this impossible. Can this be done in some

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: Memory leak issue between extern (c) and D function

2023-04-15 Thread Ali Çehreli via Digitalmars-d-learn
On 4/13/23 20:50, backtrack wrote: > mystruct* getmystruct() > { > mystruct* mystruct = cast(mystruct*)malloc(mystruct.sizeof); > return mystruct; > > } There must be a corresponding function to do the cleaning: void freemystruct(mystruct* ptr) { free ptr;