www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const of AliasSeq is silently ignored

reply Yuxuan Shui <yshuiv7 gmail.com> writes:
In this example:

     const(AliasSeq!(int, int)) a;
     pragma(msg, typeof(a)); // (int, int)

This kind of make sense, since AliasSeq is not a "single" type. 
But silently dropping const seems bad, the compiler should 
probably report an error/warning in this case?
Apr 08 2019
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 04/08/2019 12:56 PM, Yuxuan Shui wrote:
 
 In this example:
 
      const(AliasSeq!(int, int)) a;
I would expect that to mean a type list (int, int) that cannot be modified, meaning that it is not allowed to change it from (int, int).
      pragma(msg, typeof(a)); // (int, int)
Makes sense to me. However, there is no syntax that allows mutating an AliasSeq. In other words, the following doesn't compile anyway: AliasSeq!(int, int) a; a ~= AliasSeq!(double); So, adding const to that construct does not add any meaning but not many people would notice it. :) Ali
Apr 08 2019
prev sibling next sibling parent Alex <AJ gmail.com> writes:
On Monday, 8 April 2019 at 19:56:50 UTC, Yuxuan Shui wrote:
 In this example:

     const(AliasSeq!(int, int)) a;
     pragma(msg, typeof(a)); // (int, int)

 This kind of make sense, since AliasSeq is not a "single" type. 
 But silently dropping const seems bad, the compiler should 
 probably report an error/warning in this case?
kinda makes sense and making sense are two different things. It has to make sense to the compiler. While I see that you want to distribute const over the list, D is not designed to do this with anything that I know of. It could, without issue, but one must makes sure it does not contradict any other uses. If it doesn't then it could be a bonus feature. Normally though one expects const to work on it's argument and so this also suggests having a const AliasSeq. Since we can't have a const AliasSeq there may be no issue redefining it to me what you want. I agree that silently dropping things are bad. D does this sometimes and it can be a real pain.
Apr 09 2019
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
On Monday, 8 April 2019 at 19:56:50 UTC, Yuxuan Shui wrote:
 In this example:

     const(AliasSeq!(int, int)) a;
     pragma(msg, typeof(a)); // (int, int)

 This kind of make sense, since AliasSeq is not a "single" type. 
 But silently dropping const seems bad, the compiler should 
 probably report an error/warning in this case?
It works if you use the "storage class" syntax for const: const AliasSeq!(int, int) a; pragma(msg, typeof(a)); // (const(int), const(int))
Apr 09 2019
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/8/19 3:56 PM, Yuxuan Shui wrote:
 
 In this example:
 
      const(AliasSeq!(int, int)) a;
      pragma(msg, typeof(a)); // (int, int)
 
 This kind of make sense, since AliasSeq is not a "single" type. But 
 silently dropping const seems bad, the compiler should probably report 
 an error/warning in this case?
I agree with you, please file a bug. I would have expected it to be const(int), const(int). I would expect this pattern to always hold, no matter what T is. T var1; const(T) var2; static assert(is(typeof(var2) == const)); -Steve
Apr 11 2019