www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - private method callable from other module

reply kdevel <kdevel vogtner.de> writes:
Is this a known bug?

```
// file b.d
import std.stdio;

struct S {
    private void foo (ubyte c)
    {
       writeln (__PRETTY_FUNCTION__);
    }
    void foo ()
    {
    }
}
```

```a.d
// file a.d
unittest {
    import b;
    auto s = S ();
    s.foo ('x');
}
```

```
$ dmd -g -unittest -main a.d b.d
$ ./a
void b.S.foo(ubyte c)
1 modules passed unittests
```
Oct 08 2022
parent reply kdevel <kdevel vogtner.de> writes:
On Saturday, 8 October 2022 at 09:46:59 UTC, kdevel wrote:

 ```
 $ dmd -g -unittest -main a.d b.d
 $ ./a
 void b.S.foo(ubyte c)
 1 modules passed unittests
 ```
2.086.1 a.d(5): Error: struct `b.S` member foo is not accessible 2.090.1 compiles
Oct 08 2022
parent reply kdevel <kdevel vogtner.de> writes:
On Saturday, 8 October 2022 at 09:59:17 UTC, kdevel wrote:
 On Saturday, 8 October 2022 at 09:46:59 UTC, kdevel wrote:

 ```
 $ dmd -g -unittest -main a.d b.d
 $ ./a
 void b.S.foo(ubyte c)
 1 modules passed unittests
 ```
2.086.1 a.d(5): Error: struct `b.S` member foo is not accessible 2.090.1 compiles
Interestingly (again with dmd v2.100.2) `-run` runs into a link error: ``` $ dmd -g -unittest -main -run a.d b.d a.o: In function `_D1a16__unittest_L2_C1FZv': (...)./a.d:5: undefined reference to `_D1b1S3fooMFhZv' collect2: error: ld returned 1 exit status Error: linker exited with status 1 ```
Oct 08 2022
next sibling parent reply Jack Pope <zedlan invec.net> writes:
Altering the definition sequence in b.d to have the private 
version second has the desired result. Otherwise, when following 
private foo, it looks like public foo gets adopted as a public 
constructor. The same for classes.
Oct 08 2022
parent kdevel <kdevel vogtner.de> writes:
On Saturday, 8 October 2022 at 15:56:01 UTC, Jack Pope wrote:
 Altering the definition sequence in b.d to have the private 
 version second has the desired result. Otherwise, when 
 following private foo, it looks like public foo gets adopted as 
 a public constructor. The same for classes.
Created Issue 23397 - private method callable from other module
Oct 08 2022
prev sibling parent kdevel <kdevel vogtner.de> writes:
On Saturday, 8 October 2022 at 10:05:33 UTC, kdevel wrote:
 ```
 $ dmd -g -unittest -main -run a.d b.d
 a.o: In function `_D1a16__unittest_L2_C1FZv':
 (...)./a.d:5: undefined reference to `_D1b1S3fooMFhZv'
 collect2: error: ld returned 1 exit status
 Error: linker exited with status 1
 ```
`b.d` is the cmd line arg to the executable. The correct order of arguments to dmd is: `dmd -g -unittest -main b.d -run a.d`
Oct 08 2022