Is there a way to enforce UFCS?

thebluepandabear therealbluepandabear at protonmail.com
Wed Jan 4 03:42:28 UTC 2023


Say you have the following class which represents a dog 🐶:

```D
class Dog {
     @property {
         string name();

         void name(string name) {
             _name = name;
         }
     }

     private {
         string _name;
     }
}
```

And you have the following code with constructs a `Dog` object:

```D
void main() {
     Dog d = new Dog();

     d.name = "Poodle";
     writeln(d.name);
}
```

In the code we can see that we have utilized UFCS (universal 
function call syntax) to set the properties for the object. This 
feature is great. We have also used D's `@property` annotation 
which gives us some other advantages that you can see in the 
documentation.

The issue I have is that UFCS is not enforced, which I thought 
would be a rather good use for the `@property` annotation. This 
means that we can do the following in our code:

```D
void main() {
     Dog d = new Dog();

     d.name("poodle");
     writeln(d.name());
}
```

I prefer the UFCS version over the non-UFCS version since it is 
more clear that it is a property and it matches closely with the 
official D style guide.

I am disappointed that `@property` does not enforce UFCS, as I 
believe that it would add to its usefulness. Sometimes throughout 
my codebase I get confused and write properties in non-UFCS 
syntax, which bugs me a bit.

My question is: is there a way to enforce UFCS-syntax?


More information about the Digitalmars-d-learn mailing list