First project: questions on how-to, and on language features

Chris Wright via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 24 10:52:41 PST 2016


On Sun, 24 Jan 2016 06:07:13 +0000, Alex Vincent wrote:

> Source code:
> https://alexvincent.us/d-language/samples/intervalmap-rev1.d.txt

There is no documentation, so I have no idea what you're trying to 
achieve here. So your questions about why this isn't in Phobos, whether 
there are any other libraries that do this, and whether there's a way to 
simplify your contracts are impossible for me to answer.

I notice you're using identifiers that start with double underscores. I'd 
recommend not making a habit of that. The compiler generates symbols for 
some things, and those symbols start with a double underscore.

For example:

  class Foo {
    this(int i) {}
    void __ctor(int i) {}
  }
  auto f = new Foo(1);

This produces an error like:

dubleuscore.d(13): Error: dubleuscore.Foo.__ctor called with argument 
types (int) matches both:
dubleuscore.d(4):     dubleuscore.Foo.this(int i)
and:
dubleuscore.d(7):     dubleuscore.Foo.__ctor(int i)

The compiler can add more such identifiers without warning, which could 
break your code in interesting ways.

> After reading Ali Çehreli's book, "Programming in D", I wrote this
> little sample up as a starting point for my explorations into D.  I've
> long admired the D language, but I've never actually tried to write
> practical code in D.
> 
> So I wrote this little sample up in dlangide, and as I was going,
> I realized I had quite a few questions.
> 
> (1) It's not clear how to specify certain parts of a module or library
> as non-exportable.  Is that possible?  Is it desirable? (It's not that
> important, yet, but still...)

As Marc mentioned, the 'private' keyword. You can use it directly:

  private int f;
  int g;  // implicitly public

in blocks:

  private {
    int h;
    int i;
  }
  int j;  // implicitly public

or label style:

  private:
  int k;
  int m;
  public int n;  // have to explicitly mark anything else public

That's all module-local. If you need to restrict things to your package, 
you can use the 'package' keyword, which functions identically.

All these syntax variants work everywhere. I find it handy, for instance, 
to write my code like:

  class Foo {
    private {
      // fields go here
    }
    // everything else is generally public
  }

Aside from that, if you do not add a documentation comment to something, 
it will not be mentioned at all in the generated documentation. This 
makes it difficult for users to discover, in case you really need 
something to be public but don't want people to use it. But using 
'package' rather than 'public' will usually suffice in those cases.

> (3) How do I actually create a library in dub?  How does dub determine
> what files to build?

I believe Dub includes all files by default.

> (4) How should the scope(exit) and scope(failure) guard statements
> intermix with preconditions and postconditions?

Marc answered this, but while we're on the topic of preconditions, I 
notice your usage of enforce:

  enforce(index > upperEdge, new StringException("index must be greater 
than upperEdge"));

The idiom in use in Phobos is:

  enforce!StringException(index > upperEdge, "index must be greater than 
upperEdge");


More information about the Digitalmars-d-learn mailing list