Thread overview
class member access
Apr 16, 2005
Darren Grant
Apr 16, 2005
J C Calvarese
Apr 16, 2005
Derek Parnell
Apr 16, 2005
Darren GRant
April 16, 2005
Hello,

New to D, coming from C++.  Why does the following code compile and run?  I was expecting an error about A.x not being accessible.


// test.d

class A
{
private
{
int x;
}
}

void main()
{
A a = new A;
a.x = 0;
}


Compiled with the dmd.exe dated Apr 6,2005-- don't know how to get the version number from the program, is there a way? ;)

Compile cmd line:

dmd -I. -debug -g -inline -unittest -c -oftest.obj test.d
dmd -oftest.exe test.obj -debug -g -inline -unittest
C:\dmd\bin\..\..\dm\bin\link.exe test,test.exe,,user32+kernel32/co/noi;


Regards,
Darren Grant
April 16, 2005
Aha, no sooner do I post this then I discover the answer in the docs.  Elegant.

Still outstanding, is there a way to query dmd.exe for its build version?


In article <d3s466$g3q$1@digitaldaemon.com>, Darren Grant says...
>
>Hello,
>
>New to D, coming from C++.  Why does the following code compile and run?  I was expecting an error about A.x not being accessible.
>
..snip...


Regards,
Darren Grant
April 16, 2005
On Sat, 16 Apr 2005 22:39:02 +0000 (UTC), Darren Grant wrote:

> Hello,
> 
> New to D, coming from C++.  Why does the following code compile and run?  I was expecting an error about A.x not being accessible.
> 
> 
> // test.d
> 
> class A
> {
> private
> {
> int x;
> }
> }
> 
> void main()
> {
> A a = new A;
> a.x = 0;
> }
>

The attribute 'private' works a little differently in D. It means that anything that is made 'private' can only be seen by code in the same module. And the module is the source file in D. If you need to have 'x' hidden from the code in 'main', place the class definition in a different file (module) and import that module into test.d

[classA.d]
module classA;
class A
{
 private
 {
 int x;
 }
 }


[test.d]
import classA;
 void main()
 {
 A a = new A;
 a.x = 0;
 }

To compile this you then need to do ...

  dmd test.d classA.d

or use my Build utility ...

 build test


> 
> Compiled with the dmd.exe dated Apr 6,2005-- don't know how to get the version number from the program, is there a way? ;)

Just use 'dmd' by itself on the command line ...

C:\> dmd
Digital Mars D Compiler v0.120
Copyright (c) 1999-2005 by Digital Mars written by Walter Bright
Documentation: www.digitalmars.com/d/index.html
Usage:
  dmd files.d ... { -switch }

  files.d        D source files
  -c             do not link
  -d             allow deprecated features
  -g             add symbolic debug info
  -v             verbose
  -O             optimize
  -odobjdir      write object files to directory objdir
  -offilename    name output file to filename
  -op            do not strip paths from source file
  -Ipath         where to look for imports
  -Llinkerflag   pass linkerflag to link
  -debug         compile in debug code
  -debug=level   compile in debug code <= level
  -debug=ident   compile in debug code identified by ident
  -inline        do function inlining
  -profile       profile runtime performance of generated code
  -release       compile release version
  -unittest      compile in unit tests
  -version=level compile in version code >= level
  -version=ident compile in version code identified by ident
  -w             enable warnings


-- 
Derek Parnell
Melbourne, Australia
http://www.dsource.org/projects/build
17/04/2005 9:09:03 AM
April 16, 2005
In article <1nleuv2cnlva4$.15ghytrtaeb09$.dlg@40tude.net>, Derek Parnell says...
>
>The attribute 'private' works a little differently in D. It means that anything that is made 'private' can only be seen by code in the same module. And the module is the source file in D. If you need to have 'x' hidden from the code in 'main', place the class definition in a different file (module) and import that module into test.d

That is very practical.  I think I will enjoy working with this much more than the C++ friend kludge. ;)


>> Compiled with the dmd.exe dated Apr 6,2005-- don't know how to get the version number from the program, is there a way? ;)
>
>Just use 'dmd' by itself on the command line ...
>
>C:\> dmd Digital Mars D Compiler v0.120
..snip...

Ah many thanks Derek!  I missed the version line.


Regards,
Darren Grant
April 16, 2005
darn_at_trideja_dot_com wrote:
> Aha, no sooner do I post this then I discover the answer in the docs.  Elegant.
> 
> Still outstanding, is there a way to query dmd.exe for its build version?

[snip]

If you type just "dmd" at the command line, all kinds of useful facts appear (including the version number). ;)

H:\Documents and Settings\jcc>dmd
Digital Mars D Compiler v0.121
Copyright (c) 1999-2005 by Digital Mars written by Walter Bright
Documentation: www.digitalmars.com/d/index.html
Usage:
  dmd files.d ... { -switch }

  files.d        D source files
  -c             do not link
  -d             allow deprecated features
  -g             add symbolic debug info
  -v             verbose
  -O             optimize
  -odobjdir      write object files to directory objdir
  -offilename    name output file to filename
  -op            do not strip paths from source file
  -Ipath         where to look for imports
  -Llinkerflag   pass linkerflag to link
  -debug         compile in debug code
  -debug=level   compile in debug code <= level
  -debug=ident   compile in debug code identified by ident
  -inline        do function inlining
  -profile       profile runtime performance of generated code
  -release       compile release version
  -unittest      compile in unit tests
  -version=level compile in version code >= level
  -version=ident compile in version code identified by ident
  -w             enable warnings

-- 
jcc7
http://jcc_7.tripod.com/d/
April 17, 2005
darn_at_trideja_dot_com wrote:

> Still outstanding, is there a way to query dmd.exe for its build version?

There is, but it would be more useful to specify it in the *code* ?

That way, D code that uses experimental features like '$' for arrays
could (somehow) require that in the source code, to make it easier...

Unfortunately, "version" can only have a on/off and not any values.
So you can do "version(DigitalMars)", but not "version(DMD >= 0.116)"

--anders