www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - import issue?

reply DLearner <bmqazwsx123 gmail.com> writes:
Please,
Why does:

```
// Test module Ex_mod
struct SA {
    int   SAIntFld1;
    int   SAIntFld2;
}

bool AddEle(ref void* StartPtr, SA PayLoad1) {

    import core.stdc.stdlib : malloc;

    struct Ele {
       SA   PayLoad;
       Ele* EleNxtPtr;
       Ele* ElePrvPtr;
    }

    Ele*    ElePtr;
    Ele*  wkElePtr;

    return true;
}

```

imported into:
```
// Test harness

struct SA {
    int   SAIntFld1;
    int   SAIntFld2;
}


void main() {

    import std.stdio: writeln;

    import Ex_mod;

    SA SAVar;
    void* SA_StartPtr = null;

    SAVar.SAIntFld1 = 3;
    SAVar.SAIntFld2 = -5;

    if (AddEle(SA_StartPtr, SAVar)) {
       writeln("Element linked");
    } else {
       writeln("Element not linked");
    }

}
```

Fail with:
```
ex_main.d(21): Error: function `Ex_mod.AddEle(ref void* StartPtr, 
SA PayLoad1)` is not callable using argument types `(void*, SA)`
ex_main.d(21):        cannot pass argument `SAVar` of type 
`ex_main.SA` to parameter `Ex_mod.SA PayLoad1`

```

When eliminating the import via:

```
// Test harness

struct SA {
    int   SAIntFld1;
    int   SAIntFld2;
}

bool AddEle(ref void* StartPtr, SA PayLoad1) {

    import core.stdc.stdlib : malloc;

    struct Ele {
       SA   PayLoad;
       Ele* EleNxtPtr;
       Ele* ElePrvPtr;
    }

    Ele*    ElePtr;
    Ele*  wkElePtr;

    return true;
}


void main() {

    import std.stdio: writeln;

//   import Ex_mod;

    SA SAVar;
    void* SA_StartPtr = null;

    SAVar.SAIntFld1 = 3;
    SAVar.SAIntFld2 = -5;

    if (AddEle(SA_StartPtr, SAVar)) {
       writeln("Element linked");
    } else {
       writeln("Element not linked");
    }

}
```

works correctly?
Nov 22 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
You have two ``SA`` structs, each in different encapsulations.

Each of them are different, even if they have similar members.

In D types that look the same do not combine, they are distinct.

You can see this by comparing the mangling of each.

``pragma(msg, SA.mangleof);``
Nov 22 2023
parent reply DLearner <bmqazwsx123 gmail.com> writes:
On Wednesday, 22 November 2023 at 16:11:03 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 You have two ``SA`` structs, each in different encapsulations.

 Each of them are different, even if they have similar members.

 In D types that look the same do not combine, they are distinct.

 You can see this by comparing the mangling of each.

 ``pragma(msg, SA.mangleof);``
Is the encapsulation issue resolved if the struct itself is held in another module, and imported from that module into both the 'main' and 'Ex_mod' files?
Nov 22 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 23/11/2023 5:34 AM, DLearner wrote:
 Is the encapsulation issue resolved if the struct itself is held in 
 another module, and imported from that module into both the 'main' and 
 'Ex_mod' files?
Each module is its own encapsulation unit. As long as you are using the same distinct type in both modules, the issues are resolved. Where the distinct type is defined does not matter for matching of function parameters.
Nov 22 2023
parent DLearner <bmqazwsx123 gmail.com> writes:
On Wednesday, 22 November 2023 at 16:51:54 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 On 23/11/2023 5:34 AM, DLearner wrote:
 Is the encapsulation issue resolved if the struct itself is 
 held in another module, and imported from that module into 
 both the 'main' and 'Ex_mod' files?
Each module is its own encapsulation unit. As long as you are using the same distinct type in both modules, the issues are resolved. Where the distinct type is defined does not matter for matching of function parameters.
OK thanks.
Nov 22 2023