digitalmars.D.learn - canFind typing
- bearophile (15/15) Nov 29 2012 I have used std.algorithm.canFind with different tuple types by
- Jonathan M Davis (4/21) Nov 29 2012 Why wouldn't they be? You can compare int and double, and that's what fi...
- bearophile (15/17) Nov 29 2012 Right, it's a matter of equality operator.
I have used std.algorithm.canFind with different tuple types by mistake, and the compiler has not complained. So to show it I have written some reduced code that uses just numbers: import std.algorithm: canFind; void main() { int[] arr1 = [1, 2]; double x1 = 2.0; assert(arr1.canFind(x1)); double[] arr2 = [1.0, 2.0]; int x2 = 2; assert(arr2.canFind(x2)); } Are both of those canFind calls acceptable? Bye, bearophile
Nov 29 2012
On Thursday, November 29, 2012 15:28:56 bearophile wrote:I have used std.algorithm.canFind with different tuple types by mistake, and the compiler has not complained. So to show it I have written some reduced code that uses just numbers: import std.algorithm: canFind; void main() { int[] arr1 = [1, 2]; double x1 = 2.0; assert(arr1.canFind(x1)); double[] arr2 = [1.0, 2.0]; int x2 = 2; assert(arr2.canFind(x2)); } Are both of those canFind calls acceptable?Why wouldn't they be? You can compare int and double, and that's what find and canFind care about. - Jonathan M Davis
Nov 29 2012
Jonathan M Davis:Why wouldn't they be? You can compare int and double, and that's what find and canFind care about.Right, it's a matter of equality operator. In my code I was performing canFind on an array of tuples. So I didn't realize that the following code (where both tuple field type and field name are different) is accepted in D (probably I am getting used to the higher type strictness of functional languages): import std.typecons: Tuple; alias T1 = Tuple!(int, "x"); alias T2 = Tuple!(double, "y"); void main(string[] args) { assert(T1(1) == T2(1)); } Bye, bearophile
Nov 29 2012