www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Non-D linkage with out and inout parameters.

reply Jeremie Pelletier <jeremiep gmail.com> writes:
I would like to propose a change in the behavior of out and inout parameters on
functions declared without the D linkage type so they behave just like they
would in their native language, and the keywords are kept as compiler hints
rather than automatically handling the pointer. So for example, it wouldn't be
necessary for the compiler to initialize the value to a out parameter but yet
allow explicit dereference in the call.

Here's an example to illustrate the syntax:
---
extern(Windows) void GetLocalTime(out LPSYSTEMTIME lpSystemTime);

unittest {
    /// D linkage, normal out behavior
    void MyGetLocalTime(out SYSTEMTIME st) {
       /// stdcall linkage, native out behavior
       GetLocalTime(&st);
    }

    SYSTEMTIME st; /// Should not get initialized here
    MyGetLocalTime(st);
}
---

The main rationale behind it is to keep native behavior to functions we've
already been using for years while not having to modify the parameters
declaration for D functions. So it wouldn't break existing code to add out and
inout to imported functions, it would just allow for better optimizations and
documentations.
Mar 08 2009
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Jeremie Pelletier wrote:
 I would like to propose a change in the behavior of out and inout parameters
on functions declared without the D linkage type so they behave just like they
would in their native language, and the keywords are kept as compiler hints
rather than automatically handling the pointer. So for example, it wouldn't be
necessary for the compiler to initialize the value to a out parameter but yet
allow explicit dereference in the call.
 
 Here's an example to illustrate the syntax:
 ---
 extern(Windows) void GetLocalTime(out LPSYSTEMTIME lpSystemTime);
 
 unittest {
     /// D linkage, normal out behavior
     void MyGetLocalTime(out SYSTEMTIME st) {
        /// stdcall linkage, native out behavior
        GetLocalTime(&st);
     }
 
     SYSTEMTIME st; /// Should not get initialized here
     MyGetLocalTime(st);
 }
 ---
 
 The main rationale behind it is to keep native behavior to functions we've
already been using for years while not having to modify the parameters
declaration for D functions. So it wouldn't break existing code to add out and
inout to imported functions, it would just allow for better optimizations and
documentations.
 

I agree with the proposal, but for the optimization use case, use: SYSTEMTIME st = void; This won't initialize st to zero.
Mar 08 2009
parent Jeremie Pelletier <jeremiep gmail.com> writes:
Robert Fraser Wrote:

 I agree with the proposal, but for the optimization use case, use:
 SYSTEMTIME st = void;
 This won't initialize st to zero.

This is what I currently do but I would assume it's better suited for variables used in the current scope instead of passed as an out parameter to the next statement. So it would end up being just like doing "uint i = 0;", it works, but doing "uint i;" does the same.
Mar 08 2009