| Thread overview | |||||
|---|---|---|---|---|---|
|
November 29, 2012 canFind typing | ||||
|---|---|---|---|---|
| ||||
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
| ||||
November 29, 2012 Re: canFind typing | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
| |||
November 29, 2012 Re: canFind typing | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply