Possible change to symbol protection

bauss jj_1337 at live.dk
Wed Nov 8 02:52:14 UTC 2017


I believe it would be nice if we were allowed to pass symbols we 
don't have access to into templates, but of course we shouldn't 
be allowed to use them.

Ex.

foo.d

```
module foo;

private int bar;
```

baz.d
```
template MyTemplate(alias Symbol)
{
     ...
}

...

import foo;

MyTemplate!bar; // Okay, because we're not actually using bar 
within MyTemplate

...

template MyOtherTemplate(alias Symbol)
{
     enum MyOtherTemplate = Symbol;
}

MyOtherTemplate!bar; // Not okay, because we're using the symbol 
when we don't have access to it.
```

This would allow something like this:

```
template isVisible(alias Symbol)
{
     enum isVisible = __traits(compiles, Symbol);
}

...

static if (isVisible!bar)
{
     // bar is visible ...
}
else
{
     // bar is not visible ...
}
```

Instead of today you have to either do:

```
static if (__traits(compiles, bar))
{
     // bar is visible ...
}
else
{
     // bar is not visible ...
}
```

IMO that isn't pretty code and being able to have a template like 
"isVisible" would be much cleaner.

You can have a template like it, but it's really ugly, because it 
has to retrieve a string equivalent to the symbol name, which 
isn't typesafe at all.

```
template isVisible(string Symbol)
{
	enum isVisible = __traits(compiles, mixin(Symbol));
}

...

static if (isVisible!"bar")
{
     // bar is visible ...
}
else
{
     // bar is not visible ...
}
```



More information about the Digitalmars-d mailing list