Thread overview
[Issue 8486] New: Possibly wrong interaction of Variant and const arrays
Jul 31, 2012
cybevnm
Jul 31, 2012
klickverbot
Jul 31, 2012
klickverbot
Aug 01, 2012
cybevnm
Aug 01, 2012
klickverbot
Aug 01, 2012
cybevnm
July 31, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8486

           Summary: Possibly wrong interaction of Variant and const arrays
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: cybevnm@gmail.com


--- Comment #0 from cybevnm <cybevnm@gmail.com> 2012-07-31 12:36:58 PDT ---
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 ?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 31, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8486


klickverbot <code@klickverbot.at> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code@klickverbot.at


--- Comment #1 from klickverbot <code@klickverbot.at> 2012-07-31 13:07:32 PDT ---
Variant should generally ignore top-level const for »value types« (i.e. array slices, pointers, integral types, …).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 31, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8486



--- Comment #2 from klickverbot <code@klickverbot.at> 2012-07-31 13:09:26 PDT ---
… the point being that this is, if anything, analogous to the constness of the Variant instance itself, not the contained type.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 01, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8486



--- Comment #3 from cybevnm <cybevnm@gmail.com> 2012-08-01 12:58:14 PDT ---
(In reply to comment #2)
> … the point being that this is, if anything, analogous to the constness of the Variant instance itself, not the contained type.

Unfortunately, Variant's handling of arrays is different to handling of class references. So we have next situtation:

import std.variant;
class C { }
void main()
{
  {
    const C c;
    Variant v = Variant( c );
    assert( v.peek!( typeof(c) )() != null );
  }
  {
    const C[] cs;
    Variant v = Variant( cs );
    assert( v.peek!( typeof(cs) )() == null );
 }
}
But array top-level const cutting is understandable, it's the way it works in
all other places...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 01, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8486



--- Comment #4 from klickverbot <code@klickverbot.at> 2012-08-01 13:08:10 PDT ---
I meant that Variant should regard e.g. const(T[]) and const(T)[] or const(T*)
and const(T)* equivalent, i.e. allow implicit conversion between them.

The fact that const is automatically stripped from arrays is not Variant-specific, template functions generally work like that in D.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 01, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8486



--- Comment #5 from cybevnm <cybevnm@gmail.com> 2012-08-01 13:43:19 PDT ---
(In reply to comment #4)
> I meant that Variant should regard e.g. const(T[]) and const(T)[] or const(T*)
> and const(T)* equivalent, i.e. allow implicit conversion between them.

Agree, it will resolve the problem.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------