Algebraic changing type when in associative array

rous email at example.com
Wed Feb 5 01:06:46 UTC 2020


On Tuesday, 4 February 2020 at 12:05:16 UTC, rikki cattermole 
wrote:
> On 05/02/2020 12:55 AM, rous wrote:
>> On Tuesday, 4 February 2020 at 11:47:49 UTC, rikki cattermole 
>> wrote:
>>> It did not change type.
>>>
>>> "Algebraic data type restricted to a closed set of possible 
>>> types. It's an alias for VariantN with an 
>>> appropriately-constructed maximum size."
>>>
>>> https://dlang.org/phobos/std_variant.html#.Algebraic
>> 
>> Okay, that makes sense. However, shouldn't it still be the 
>> same Algebraic type, and thus an alias for a VariantN with the 
>> same maximum size?
>
> Algebraic doesn't exist. Its a name for your use.
>
> This is what an alias is. It does not create a new type, it 
> gives a name to another one.
>
> This chapter of the book (free) goes into more detail about 
> alias http://ddili.org/ders/d.en/alias.html

This is an example program that illustrates the issue:

import std.variant: Algebraic, This;
import std.stdio: writeln;
alias Foo = Algebraic!(This[], int);
void main()
{
     Foo x = 5;
     Foo y = 10;
     Foo z = [x,y];
     pragma(msg, typeof(x));
     pragma(msg, typeof(y));
     pragma(msg, typeof(z));
     pragma(msg, typeof(z[0]));
}
At compile time, it prints VariantN!(16LU, This[], int) three 
times and then VariantN!32LU.


More information about the Digitalmars-d-learn mailing list