D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19220 - multiSort and SortedRange.groupBy do not play together
Summary: multiSort and SortedRange.groupBy do not play together
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 All
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-09-04 21:32 UTC by Justin
Modified: 2020-04-27 14:37 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Justin 2018-09-04 21:32:25 UTC
std.algorithm.multiSort returns a SortedRange which in turn has a groupBy method.  Usage of this method does not actually compile when sorting by more than one predicate.  Example repro (online here: https://run.dlang.io/is/mQGAvD):

import std.stdio;
import std.algorithm;
void main()
{
	auto records = [
        Record(10, 10),
        Record(12, 1),
        Record(30, 12),
        Record(10, 10),
    ];
    
    {
    	// Works:
    	auto groups = records.multiSort!(`a.x < b.x`).groupBy;
        writeln(groups);
    }
    
    {
    	// Doesn't work:
    	auto groups = records.multiSort!(`a.x < b.x`, `a.y < b.y`).groupBy;
    	writeln(groups);
    }
}

struct Record
{
    int x;
    int y;
}



SortedRange.groupBy apparently delegates the work to chunkBy which chokes on the predicate defined by multiSort:

/dlang/dmd-beta/linux/bin64/../../src/phobos/std/algorithm/iteration.d(1821): Error: static assert:  "chunkBy expects either a binary predicate or a unary predicate on range elements of type: Record"
/dlang/dmd-beta/linux/bin64/../../src/phobos/std/algorithm/iteration.d(1896):        instantiated from here: ChunkByImplIsUnary!(__lambda1, Record[])
/dlang/dmd-beta/linux/bin64/../../src/phobos/std/algorithm/iteration.d(2110):        instantiated from here: ChunkByImpl!(__lambda1, Record[])
/dlang/dmd-beta/linux/bin64/../../src/phobos/std/range/package.d(10730):        instantiated from here: chunkBy!((a, b) => !predFun(a, b) && !predFun(b, a), Record[])
onlineapp.d(21):        instantiated from here: groupBy!()

Commenting out the "doesn't work" section demonstrates that multiSort's predicate works fine as long there's a single original predicate.
Comment 1 moonlightsentinel 2020-04-27 14:37:50 UTC
Compiles & runs sucessfully with 2.091.0