Suggestion: module opcall

bearophile bearophileHUGS at lycos.com
Thu Jan 8 17:23:51 PST 2009


Bill Baxter:

>I think this problem you refer to only applies to modules without package names.  I.e. "module Foo" containing "class Foo".  Things seem to work ok if you have "module x.Foo" with "class Foo" inside.<

Right. But why settle with a half-backed module system when there are more logic ways to design it? :-)


> So you are saying you want this to be an error?
> 
> import std.string;
> void foo() {
>     std.string.format("hi");  // error no such symbol "std.string"
>     format("hi");   // ok
> }


The following examples show what I mean:

import std.string;
void foo() {
    std.string.format("hi"); // OK, "std.string" name exists in the current scope.
    format("hi"); // Error, "format" name is absent.
}


import std.string: format;
void foo() {
    std.string.format("hi"); // Error, "std" name is absent.
    format("hi"); // OK.
}


import std.string: *;
void foo() {
    std.string.format("hi"); // Error, "std" name is absent.
    format("hi"); // OK, all names inside "std.string" are now present in the current scope.
}


import std.string;
import std.string: format;
void foo() {
    std.string.format("hi"); // OK.
    format("hi"); // OK.
}

This doesn't solve all problems, but it's a good step toward improving the situation.



>auto x = new Foo;  // Foo.Foo should not be needed if class Foo is the only thing inside Foo.<

That's a special casing, it may look handy, but it may also lead to troubles later. So I think it's better to keep things tidy, and avoid that. D allows enough things (renamed imports, alias, and selective imports) to solve such problems already.


> template AReallyLongTemplateNameIDontWannaTypeTwice(T) {
>      void this(T x) { }
> }

You can write that in D1 as:

void aReallyLongTemplateNameIDontWannaTypeTwice(T)(T x) {
}


>I love how I don't have to change all the constructor names in D whenever I change a class name.<

I agree, but for example when you define a opCmp of a struct you have to do:

struct Foo {
    int x;
    int opCmp(Foo other) { ...

As you can see the name Foo is repeated two times, and I don't like that much (I don't use an IDE able to refractor names).
In D1 I sometimes solve that small problem like this:

struct Foo {
    int x;
    int opCmp(typeof(*this) other) { ...

Or like this:

struct Foo {
    int x;
    alias typeof(*this) TyThis;
    int opCmp(TyThis other) { ...

Bye,
bearophile



More information about the Digitalmars-d mailing list