Bug 17982 – Support for const(Class) in algorithm.searching.extremum

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2017-11-14T09:31:49Z
Last change time
2018-04-05T06:20:31Z
Keywords
pull
Assigned to
No Owner
Creator
Aurelien Fredouelle

Comments

Comment #0 by aurelien.fredouelle+dlang — 2017-11-14T09:31:49Z
Initially reported in https://forum.dlang.org/post/[email protected] 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; }
Comment #1 by greensunny12 — 2018-02-11T00:51:16Z
PR: https://github.com/dlang/phobos/pull/6157 > In the forum thread, @vit suggested using something like this instead: Yeah that's a good idea.
Comment #2 by github-bugzilla — 2018-04-05T06:20:30Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/64d3a078d3df789734af2644d1f3c51590f3e6e7 Fix Issue 17982 - Support for const(Class) in algorithm.searching.extremum https://github.com/dlang/phobos/commit/193c61d985a9645014b2161b59ddb8692308e063 Merge pull request #6157 from wilzbach/fix-17982 Fix Issue 17982 - Support for const(Class) in algorithm.searching.extremum merged-on-behalf-of: Nathan Sashihara <[email protected]>