## digitalmars.D.learn - Set differences

• Josh (10/10) Apr 05 2014 Can anyone please explain why this works:
• monarch_dodra (14/25) Apr 06 2014 setDifference returns a lazy range, whose type depends on the
• bearophile (23/32) Apr 06 2014 This compiles:
"Josh" <moonburntm gmail.com> writes:
```Can anyone please explain why this works:

auto numbers = sequence!("n")();
auto trimmed = setDifference(setDifference(numbers, sequence!("n
* a[0]")(2)), sequence!("n * a[0]")(3));

but this doesn't?

auto numbers = sequence!("n")();
auto trimmed = setDifference(numbers, sequence!("n * a[0]")(2));
trimmed = setDifference(trimmed, sequence!("n * a[0]")(3));

Thanks,
Josh
```
Apr 05 2014
"monarch_dodra" <monarchdodra gmail.com> writes:
```On Sunday, 6 April 2014 at 05:27:44 UTC, Josh wrote:
Can anyone please explain why this works:

auto numbers = sequence!("n")();
auto trimmed = setDifference(setDifference(numbers,
sequence!("n * a[0]")(2)), sequence!("n * a[0]")(3));

but this doesn't?

auto numbers = sequence!("n")();
auto trimmed = setDifference(numbers, sequence!("n *
a[0]")(2));
trimmed = setDifference(trimmed, sequence!("n * a[0]")(3));

Thanks,
Josh

setDifference returns a lazy range, whose type depends on the
inputs. It's not just a generic array. Because of this, while
these *look* like they are just ranges of numbers:
setDifference(numbers, sequence!("n * a[0]")(2));
setDifference(trimmed, sequence!("n * a[0]")(3));

Their *types* and what goes on under the hood is *complelty*
different. Because of this, you can't use the result of the
first, to store the result of the second, the types don't match.

Try this:
auto tmpTrimmed = setDifference(numbers, sequence!("n *
a[0]")(2));
auto trimmed = setDifference(tmpTrimmed, sequence!("n *
a[0]")(3));
```
Apr 06 2014
"bearophile" <bearophileHUGS lycos.com> writes:
```Josh:

Can anyone please explain why this works:

auto numbers = sequence!("n")();
auto trimmed = setDifference(setDifference(numbers,
sequence!("n * a[0]")(2)), sequence!("n * a[0]")(3));

but this doesn't?

auto numbers = sequence!("n")();
auto trimmed = setDifference(numbers, sequence!("n *
a[0]")(2));
trimmed = setDifference(trimmed, sequence!("n * a[0]")(3));

This compiles:

void main() {
import std.stdio, std.algorithm, std.range;

auto numbers = sequence!("n")();
auto trimmed = setDifference(numbers, sequence!("n *
a[0]")(2));
auto trimmed2 = setDifference(trimmed, sequence!("n *
a[0]")(3));
pragma(msg, typeof(trimmed));
pragma(msg, typeof(trimmed2));
}

Output:

SetDifference!("a < b", Sequence!("n", Tuple!()), Sequence!("n *
a[0]", Tuple!int))
SetDifference!("a < b", SetDifference!("a < b", Sequence!("n",
Tuple!()), Sequence!("n * a[0]", Tuple!int)), Sequence!("n *
a[0]", Tuple!int))

The two trimmed have different type, because in D ranges are
template-based, so often things have different type, unless they
are identical.

Bye,
bearophile
```
Apr 06 2014