digitalmars.D.learn - import issue?
- DLearner (81/81) Nov 22 2023 Please,
- Richard (Rikki) Andrew Cattermole (5/5) Nov 22 2023 You have two ``SA`` structs, each in different encapsulations.
- DLearner (5/10) Nov 22 2023 Is the encapsulation issue resolved if the struct itself is held
- Richard (Rikki) Andrew Cattermole (6/9) Nov 22 2023 Each module is its own encapsulation unit.
- DLearner (3/12) Nov 22 2023 OK thanks.
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
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
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
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
On Wednesday, 22 November 2023 at 16:51:54 UTC, Richard (Rikki) Andrew Cattermole wrote:On 23/11/2023 5:34 AM, DLearner wrote:OK thanks.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