Type inference and overloaded functions

Namespace rswhite4 at googlemail.com
Tue Dec 10 16:37:23 PST 2013


On Tuesday, 10 December 2013 at 14:20:51 UTC, bearophile wrote:
> Namespace:
>
>> What's with {1, 2, 3}? Should be easy to implement and it's 
>> known from C/C++/Java/C#
>
> In D {} has plenty of meanings already :-)
>
> Bye,
> bearophile

You was right, it was terrible and so I tried [1, 2, 3]s as 
syntax.
It has cost me hours but now this works pretty well:

----
import std.stdio;

void foo(int[3] arr) {
	assert(is(typeof(arr) == int[3]));
}

void bar(T)(T arr) {
	assert(is(T == int[3]));
}

void main() {
	int[] arr0 = [1, 2, 3];
	assert(is(typeof(arr0) == int[]));
	assert(arr0 == [1, 2, 3]);

	int[3] arr1 = [4, 5, 6]s;
	assert(is(typeof(arr1) == int[3]));
	assert(arr1 == [4, 5, 6]);

	int[] arr2 = [7, 8, 9]s;
	assert(is(typeof(arr2) == int[3]));
	assert(arr2 == [7, 8, 9]);

	foo([1, 2, 3]);
	foo([4, 5, 6]s);

	bar([44, 55, 66]s);

	auto arr3 = [111, 222, 333];
	assert(is(typeof(arr3) == int[]));

	auto arr4 = [444, 555, 666]s;
	assert(is(typeof(arr4) == int[3]));
}
----


More information about the Digitalmars-d-learn mailing list