Bug 12569 – Better error message for std.algorithm.reduce used with two functions and a scalar seed

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2014-04-12T22:44:00Z
Last change time
2014-07-28T08:59:23Z
Keywords
diagnostic
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2014-04-12T22:44:05Z
This code compiles: void main() { import std.algorithm: min, max, reduce; import std.typecons: tuple; dchar c = 'a'; reduce!(min, max)(tuple(c, c), "hello"); // OK } But here I have forgotten to use a 2-tuple as seed for reduce: void main() { import std.algorithm: min, max, reduce; import std.typecons: tuple; dchar c = 'a'; reduce!(min, max)(c, "hello"); // error } DMD 2.066alpha gives an error inside std.algorithm, but I think it's better to add a template constraint to std.algorithm.reduce or a static if to guard against this mistake: ...\dmd2\src\phobos\std\algorithm.d(772,45): Error: no property 'Types' for type 'dchar' test.d(5,22): Error: template instance std.algorithm.reduce!(min, max).reduce!(dchar, string) error instantiating
Comment #1 by monarchdodra — 2014-04-13T19:01:28Z
(In reply to bearophile_hugs from comment #0) > This code compiles: > > void main() { > import std.algorithm: min, max, reduce; > import std.typecons: tuple; > dchar c = 'a'; > reduce!(min, max)(tuple(c, c), "hello"); // OK > } > > > But here I have forgotten to use a 2-tuple as seed for reduce: > > void main() { > import std.algorithm: min, max, reduce; > import std.typecons: tuple; > dchar c = 'a'; > reduce!(min, max)(c, "hello"); // error > } I'd be more worried about passing too *many* arguments, eg: reduce!(min, max)(tuple(c, c, c), "hello"); Technically, implementation-wise, it *should* pass, but it doesn't make sense to accept it. Currently, in HEAD, this code ices the compiler. My reduce re-write just accepts it. I'll add your test(s) to it.
Comment #2 by bearophile_hugs — 2014-04-13T19:22:57Z
(In reply to monarchdodra from comment #1) > Currently, in HEAD, this code ices the compiler. I have filed it as Issue 12574
Comment #3 by bearophile_hugs — 2014-07-28T08:59:23Z