www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The default can be used as a property is very bad design.

reply Brian <zoujiaqing gmail.com> writes:
OH NO !

sample code:

```D
import std.stdio;

struct Config
{
     string test = "aaa";
     string test2 = "bbb";
}

Config config()
{
     Config conf;
     return conf;
}

class Test
{
     void func1()
     {
         string config;

         string testConfig = config().test;

         writeln(testConfig);
     }
}
```


build error:
```shell
osx:test zoujiaqing$ dmd config.d
config.d(22): Error: function expected before (), not config of 
type string
```
Jan 24 2019
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 25 January 2019 at 02:09:29 UTC, Brian wrote:
 OH NO !
This has nothing to do with property. It is just a local variable - those always take precedence over global ones. Use the module scope operator to use one outside; the leading dot:
         string testConfig = config().test;
string testConfig = .config().test; // note the leading dot
Jan 24 2019
prev sibling parent Neia Neutuladh <neia ikeran.org> writes:
As for this being very bad design, pretty much every programming language 
with the concept of a local variable shares this trait.
Jan 24 2019