www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17982] New: Support for const(Class) in

https://issues.dlang.org/show_bug.cgi?id=17982

          Issue ID: 17982
           Summary: Support for const(Class) in
                    algorithm.searching.extremum
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: aurelien.fredouelle+dlang gmail.com

Initially reported in
https://forum.dlang.org/post/bigzmlszipzpcrpmwmiv forum.dlang.org

It is not possible to use minElement on an array of const objects:

class A
{
  int val;
}

const(A) doStuff(const(A)[] v)
{
  import std.algorithm.searching : minElement;
  return v.minElement!"a.val";
}

This gets the following compiler error:

std/algorithm/searching.d(1256,28): Error: cannot implicitly convert expression
(front(r)) of type const(A) to app.A
std/algorithm/searching.d(1286,35): Error: cannot implicitly convert expression
(r[i]) of type const(A) to app.A
std/algorithm/searching.d(1258,36): Error: template instance
std.algorithm.searching.extremum!("a.val", "a < b", const(A)[], A) error
instantiating
std/algorithm/searching.d(3345,24):        instantiated from here:
extremum!("a.val", "a < b", const(A)[])
source/app.d(11,11):        instantiated from here: minElement!("a.val",
const(A)[])

This seems to be because the extremum helper function is using Unqual!Element
to hold the current extremum, which cannot be assigned to when Element is
const.

In the forum thread,  vit suggested using something like this instead:

template RebindableOrUnqual(T){
        static if (is(T == class) || is(T == interface) || isDynamicArray!T ||
isAssociativeArray!T)alias RebindableOrUnqual = Rebindable!T;
        else alias RebindableOrUnqual = Unqual!T;
}

--
Nov 14 2017