www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Top level array constness discarding and Variant

reply cybevnm <cybevnm gmail.com> writes:
During initializing Variant, D discards top level const of array, which 
leads to little unintuitive behaviour. Consider code:

import std.stdio;
import std.variant;
void main()
{
   const int[] arr;
   Variant v = Variant( arr );
   writeln( v.peek!( typeof( arr ) )() );
   writeln( v.peek!( const(int)[] )() );
   writeln( v.type() );
}

...and output:
%dmd main.d && ./main.d
null
7FFF358AE298
const(int)[]

As you can see peek works successfully not for original array type, but 
for type without top level const. Is Variant supposed to work in that way ?
Jul 30 2012
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, July 30, 2012 23:44:56 cybevnm wrote:
 During initializing Variant, D discards top level const of array, which
 leads to little unintuitive behaviour. Consider code:
 
 import std.stdio;
 import std.variant;
 void main()
 {
 const int[] arr;
 Variant v = Variant( arr );
 writeln( v.peek!( typeof( arr ) )() );
 writeln( v.peek!( const(int)[] )() );
 writeln( v.type() );
 }
 
 ...and output:
 %dmd main.d && ./main.d
 null
 7FFF358AE298
 const(int)[]
 
 As you can see peek works successfully not for original array type, but
 for type without top level const. Is Variant supposed to work in that way ?
Probably not. When arrays are passed to templated functions, they're passed as tail-const (so the constness on the array itself - but not its elements - is stripped), which in general is _way_ more useful than passing them as fully const. However, Variant predates that behavior by quite a while, and it's well-passed due for having extensive work done on its implementation (it's API should be fine, but it was implemented when D was much younger, and we can do a much better job of it now). There's a discussion on that in the main newsgroup at the moment actually. In any case, please create a bug report for this: http://d.puremagic.com/issues - Jonathan M Davis
Jul 30 2012
parent "cybevnm" <cybevnm gmail.com> writes:
On Monday, 30 July 2012 at 20:56:30 UTC, Jonathan M Davis wrote:
 On Monday, July 30, 2012 23:44:56 cybevnm wrote:
 During initializing Variant, D discards top level const of 
 array, which
 leads to little unintuitive behaviour. Consider code:
 
 import std.stdio;
 import std.variant;
 void main()
 {
 const int[] arr;
 Variant v = Variant( arr );
 writeln( v.peek!( typeof( arr ) )() );
 writeln( v.peek!( const(int)[] )() );
 writeln( v.type() );
 }
 
 ...and output:
 %dmd main.d && ./main.d
 null
 7FFF358AE298
 const(int)[]
 
 As you can see peek works successfully not for original array 
 type, but
 for type without top level const. Is Variant supposed to work 
 in that way ?
Probably not. When arrays are passed to templated functions, they're passed as tail-const (so the constness on the array itself - but not its elements - is stripped), which in general is _way_ more useful than passing them as fully const. However, Variant predates that behavior by quite a while, and it's well-passed due for having extensive work done on its implementation (it's API should be fine, but it was implemented when D was much younger, and we can do a much better job of it now). There's a discussion on that in the main newsgroup at the moment actually. In any case, please create a bug report for this: http://d.puremagic.com/issues - Jonathan M Davis
http://d.puremagic.com/issues/show_bug.cgi?id=8486
Jul 31 2012