www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - property API design

reply DanielG <simpletangent gmail.com> writes:
I'm currently wrapping a C++ API, and there are implicit 
properties in the API surface (getter and setter), but the getter 
naming is inconsistent.

The setters are always `setProperty`, but getters can be:
   - `T someProperty()`
   - `bool isSomeProperty()`
   - `bool hasSomeProperty()`

In writing my D wrapper for this, I'm hesitant to hide the 
original API entirely - not least because I would prefer people 
can consult the C++ documentation to see what methods should be 
called, without having to read with a fine-tooth comb to figure 
out whether something is a property or not (and thus having to 
omit `is`/`has`/`set` prefixes).

Often a class will have a dozen `setXXX` methods - some of them 
properties, some not. Seems a bit cruel to make half of them 
disappear, because they're property setters (requiring `.XXX = 
value` syntax instead)

**So my question is this:** would the following be acceptable in 
my D bindings? Would this lead to any annoying/weird problems 
later on?

```d
     bool isSomeProperty() { ... }
     void setSomeProperty(bool value) { ... }
      property alias someProperty = isSomeProperty;
      property alias someProperty = setSomeProperty;
```

On the surface this seems like a nice way to make using the API 
less of a chore. Property syntax if you want it, identical C++ 
syntax if not.
Sep 12
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 13/09/2026 9:41 AM, DanielG wrote:
 I'm currently wrapping a C++ API, and there are implicit properties in 
 the API surface (getter and setter), but the getter naming is inconsistent.
 
 The setters are always `setProperty`, but getters can be:
    - `T someProperty()`
    - `bool isSomeProperty()`
    - `bool hasSomeProperty()`
 
 In writing my D wrapper for this, I'm hesitant to hide the original API 
 entirely - not least because I would prefer people can consult the C++ 
 documentation to see what methods should be called, without having to 
 read with a fine-tooth comb to figure out whether something is a 
 property or not (and thus having to omit `is`/`has`/`set` prefixes).
 
 Often a class will have a dozen `setXXX` methods - some of them 
 properties, some not. Seems a bit cruel to make half of them disappear, 
 because they're property setters (requiring `.XXX = value` syntax instead)
 
 **So my question is this:** would the following be acceptable in my D 
 bindings? Would this lead to any annoying/weird problems later on?
 
 ```d
      bool isSomeProperty() { ... }
      void setSomeProperty(bool value) { ... }
       property alias someProperty = isSomeProperty;
       property alias someProperty = setSomeProperty;
 ```
 
 On the surface this seems like a nice way to make using the API less of 
 a chore. Property syntax if you want it, identical C++ syntax if not.
Forget property, that's on the kill list as it doesn't do anything useful for properties and all attempts to get it doing something useful have failed. Otherwise its a fine idea.
Sep 12
parent reply DanielG <simpletangent gmail.com> writes:
On Saturday, 12 September 2026 at 21:45:19 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 Forget  property, that's on the kill list as it doesn't do 
 anything useful for properties and all attempts to get it doing 
 something useful have failed. Otherwise its a fine idea.
I see, thank you!
Sep 12
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Saturday, 12 September 2026 at 22:12:25 UTC, DanielG wrote:
 On Saturday, 12 September 2026 at 21:45:19 UTC, Richard (Rikki) 
 Andrew Cattermole wrote:
 Forget  property, that's on the kill list as it doesn't do 
 anything useful for properties and all attempts to get it 
 doing something useful have failed. Otherwise its a fine idea.
I see, thank you!
 ` property alias`
also note aliases aint real ```d import std; "hi" void foo() {} "bye" alias bar=foo; unittest{ getUDAs!(foo,string).writeln;// hi getUDAs!(bar,string).writeln;// also hi } ```
Sep 12
parent DanielG <simpletangent gmail.com> writes:
On Saturday, 12 September 2026 at 22:37:00 UTC, monkyyy wrote:
 also note aliases aint real
That should be OK for my use-case - not doing anything fancy. As long as human-written client code 'sees' `.propertyName` I'm happy.
Sep 12