www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I need an Easy example to understand Alias This

reply Marcone <marcone email.com> writes:
Hi, I study Dlang for one year, and I can't understand alias 
this. I need an Easy example to understand Alias This.
Jul 06 2020
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 7/6/20 5:35 PM, Marcone wrote:
 Hi, I study Dlang for one year, and I can't understand alias this. I 
 need an Easy example to understand Alias This.
Is the following example useful? http://ddili.org/ders/d.en/alias_this.html Ali
Jul 06 2020
parent reply Marcone <marcone email.com> writes:
On Tuesday, 7 July 2020 at 00:42:40 UTC, Ali Çehreli wrote:
 On 7/6/20 5:35 PM, Marcone wrote:
 Hi, I study Dlang for one year, and I can't understand alias 
 this. I need an Easy example to understand Alias This.
Is the following example useful? http://ddili.org/ders/d.en/alias_this.html Ali
I can't undestand it. I need a simple example.
Jul 06 2020
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 7/6/20 5:44 PM, Marcone wrote:
 On Tuesday, 7 July 2020 at 00:42:40 UTC, Ali =C3=87ehreli wrote:
 On 7/6/20 5:35 PM, Marcone wrote:
 Hi, I study Dlang for one year, and I can't understand alias this. I =
 need an Easy example to understand Alias This.
Is the following example useful? =C2=A0 http://ddili.org/ders/d.en/alias_this.html Ali
=20 I can't undestand it. I need a simple example.
I find the example of converting two integers to double to be a simple=20 example of 'alias this'. (If it's not simple, then there is another=20 example later.) So, allow me to change the name of the struct. :) Let's say we have a=20 Money class that stores dollars and cents as two separate members. This=20 class also has a member function that returns the amount as double: struct Money { long dollars; long cents; double value() const { return double(cents) / 100 + dollars; } } unittest { assert(Money(10, 50).value =3D=3D 10.5); } void main() { } So far so good: We can get the value by calling value(). *If* this is a type where conversion to double should be automatic, then = calling value() all over the place might be considered cumbersome and=20 too wordy. If that's true, we may want to get the value of this type=20 automatically as double. For example, we may want to be able to call the = following function with Money: void foo(double d) { // ... } void main() { foo(Money(20, 50)); // <-- Compilation error } Currently the code fails to compile because Money is not 'double'.=20 (Note: Arguably, Money is not a type that should be converted to double=20 automatically, but again, I find this example simple.) 'alias this' allows a type to be converted to a specific type=20 automatically. For example, if we want to convert Money objects=20 automatically to 'double', then we use the following line inside the=20 struct defition: alias value this; What that line means is this: "Dear compiler, please automatically=20 convert this type to whatever the type of the 'value' member is." In=20 this case, "Allow using an object of Money wherever that object is used=20 in place of a double." So, with that addition, now the call inside main compiles and foo() gets = called with 'Money(20, 50).value' (compiler injects an automatic call to = value()). Here is the complete program: struct Money { long dollars; long cents; double value() const { return double(cents) / 100 + dollars; } alias value this; // <-- ADDED } unittest { assert(Money(10, 50).value =3D=3D 10.5); } void foo(double d) { // ... } void main() { foo(Money(20, 50)); } 'alias this' is sometimes used where a type needs to behave like some=20 other type with some added functionality. For example, here is another=20 example where a Month type should behave like an 'int' but it cannot=20 have any value other than 1-12: struct Month { int value; this(int value) { this.value =3D value; } invariant() { assert(value >=3D 1); assert(value <=3D 12); } alias value this; } unittest { import std.exception; assertThrown!Error(Month(0)); assertThrown!Error(Month(13)); void foo(int) { } // 'alias this' allows the following call to foo() assert(__traits(compiles, foo(Month(3)))); } void main() { } Ali
Jul 06 2020
prev sibling parent Cym13 <cpicard purrfect.fr> writes:
On Tuesday, 7 July 2020 at 00:44:32 UTC, Marcone wrote:
 On Tuesday, 7 July 2020 at 00:42:40 UTC, Ali Çehreli wrote:
 On 7/6/20 5:35 PM, Marcone wrote:
 Hi, I study Dlang for one year, and I can't understand alias 
 this. I need an Easy example to understand Alias This.
Is the following example useful? http://ddili.org/ders/d.en/alias_this.html Ali
I can't undestand it. I need a simple example.
This only scrapes the surface, but it should give an idea of the core mechanics and why it's regarded as an important concept. import std.stdio; struct Fruit { string name; } struct ColoredFruit { Fruit _fruit; alias _fruit this; string color; } void printColoredFruit(ColoredFruit f) { writeln(f.color, " ", f.name); } void printGeneralFruit(Fruit f) { writeln(f.name); } void main(string[] args) { ColoredFruit banana; banana.color = "yellow"; // It's a normal struct for its non-alias members banana.name = "banana"; // We can interact with internal fields directly // This function accepts a ColoredFruit so the whole banana is passed. printColoredFruit(banana); // > yellow banana // This function only doesn't accept a ColoredFruit, but it does accept a // Fruit and we have a Fruit alias this so the internal _fruit is passed. printGeneralFruit(banana); // > banana }
Jul 07 2020
prev sibling parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Tuesday, 7 July 2020 at 00:35:38 UTC, Marcone wrote:
 Hi, I study Dlang for one year, and I can't understand alias 
 this. I need an Easy example to understand Alias This.
I used it for widget inheritance in my experimental code here, https://github.com/aferust/noobgui/blob/master/source/widget.d
Jul 07 2020