typedef alternative
sybrandy
sybrandy at gmail.com
Sat Aug 14 18:28:56 PDT 2010
I can't recall if you can do this with structs, but if you use classes,
you should be able to define a "Date Part" class that has all of the
methods and data you want. Then, you just derive three sub-classes.
For example (and I hope the syntax is right):
class DatePart
{
this(int f)
{
_foo = f;
}
int asNumber() { return _foo; }
immutable int _foo;
}
class Year: DatePart {}
class Month: DatePart {}
class Day: DatePart {}
Casey
On 08/14/2010 08:47 PM, Yao G. wrote:
> Hello.
>
> Is there an alternative to typedef? I need to define three structs that
> have exactly the same methods and properties, yet represent different
> stuff.
>
> Example:
> ---
> struct Foo
> {
> this( int f ) {
> _foo = f;
> }
>
> int asNumber() {
> return _foo;
> }
>
> private immutable int _foo;
> }
>
> // This should be typedef, but as it's going to be deprecated...
> alias Foo Year;
> alias Foo Month;
> alias Foo Day;
>
>
> void bar( Year y, Month m, Day d ) {
> // ...
> }
>
> // This should compile, obviously.
> bar( Year(2010), Month(8), Day(14) );
>
> // But this shouldn't
> bar( Day(1), Year(1), Month(1) );
> ---
>
> So, what options do I have? I know that typedef is scheduled to
> deprecation, so I would like to know if there's something else.
>
>
More information about the Digitalmars-d-learn
mailing list