www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Add property-like Function to Type ?

reply Rubn <where is.this> writes:
I was wondering if I could create my own property in a way that 
can be used the same way as something like "T.sizeof". Right now 
I have the following to replace length:

uint length32(T)(T[] array)
{
     return cast(uint)array.length;
}

I want something similar to be able to do the following:


uint size = T.sizeof32;

The closest I can think of is doing:

uint size = sizeof32!T


That's the best I can do, which is fine but I was wondering if 
there's any other way around that?
Apr 24 2018
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Tuesday, April 24, 2018 21:36:19 Rubn via Digitalmars-d-learn wrote:
 I was wondering if I could create my own property in a way that
 can be used the same way as something like "T.sizeof". Right now
 I have the following to replace length:

 uint length32(T)(T[] array)
 {
      return cast(uint)array.length;
 }

 I want something similar to be able to do the following:


 uint size = T.sizeof32;

 The closest I can think of is doing:

 uint size = sizeof32!T


 That's the best I can do, which is fine but I was wondering if
 there's any other way around that?
If you're dealing with a user-defined type, you can declare an enum or static function on it to do something like T.sizeof32. However, you can't add stuff like that without editing the type itself, so it won't work with any types that you aren't defining yourself - especially built-in types. If you're willing to use an instance of the type, then you can declare a free function and use UFCS, but as soon as you want to use the type itself, you're going to need to use a template, which means something like sizeof32!T rather than T.sizeof32. - Jonathan M Davis
Apr 24 2018
prev sibling parent Meta <jared771 gmail.com> writes:
On Tuesday, 24 April 2018 at 21:36:19 UTC, Rubn wrote:
 I was wondering if I could create my own property in a way that 
 can be used the same way as something like "T.sizeof". Right 
 now I have the following to replace length:

 uint length32(T)(T[] array)
 {
     return cast(uint)array.length;
 }

 I want something similar to be able to do the following:


 uint size = T.sizeof32;

 The closest I can think of is doing:

 uint size = sizeof32!T


 That's the best I can do, which is fine but I was wondering if 
 there's any other way around that?
If you don't control T and can't add members, then the best thing you can probably do is instead write T.init.sizeof32. Actually, though, you can be sneaky about it and use a local function to shadow Test, but this is probably more trouble than it's worth: import std.stdio; struct Test { } property sizeof32(Test t) { return 1; } void main() { property Test() { return .Test.init; } writeln(Test.sizeof32); } This is really annoying, though, because you have to declare the Test function in every function or struct/class definition you use it in. You can create a mixin that does it automatically, but you'd still have to do `mixin(testProperties)` (where testProperties is an enum you've defined that's just the function definition).
Apr 24 2018