A little of coordination for Rosettacode

bearophile bearophileHUGS at lycos.com
Mon Mar 4 16:20:35 PST 2013


Jos van Uden:

> It depends on how you interpret it. The task describes two 
> actions:
> display and append. The question is: does the last sentence 
> refer to
> the append action or to both display and append. I choose to 
> think it
> refers to the append action because that makes more sense.

OK.

- - - - - - - - - - - -

> As for http://rosettacode.org/wiki/Simple_database
>
> You removed the struct that I used to encapsulate the functions.
>
> Aren't these functions now exposed to other modules? I wanted
> them to only be callable from main. The input validation relies
> on that.
>
> At first I had declared them all private, but then I thought it
> would be convenient to put a struct around them and declare it
> private. Maybe there's a better way.

D isn't a pure OOP language, it thankfully supports free 
functions. Generally structs and classes shouldn't be used if 
they are not useful (this is well known in the Python community). 
A struct with just static methods is essentially a namespace. In 
D modules are namespaces, so there is less need to wrap static 
functions in a struct.

In this case of the Simple database Task I think this is not a 
module to be imported, because it has a main. It's a complete 
program (like other hundreds of D Tasks in Rosettacode) and it's 
not meant to be imported. So I think wrapping everything in a 
struct in this case is useless, it just increases the indenting.

Do you agree?

- - - - - - - - - - - -

Extra note: in Rosettacode there are also few tasks meant as 
modules to be imported and used. They have the main wrapped like 
this:

version (task_name_main) {
     void main() {
     }
}


Unfortunately in D there is no built-in way to make a module that 
has a main() when it's compiled as "main module" (or when it's 
compiled as stand alone) and masks its main() when it's imported 
by another module. This is done in Python with the "if __name__ 
== __main__:" idiom. Some people say that a similar idiom is 
ugly, but it's far better to have such not nice looking but 
standard idiom, than having nothing and using a nonstandard 
version() like that.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list