This needs to be fixed

monkyyy crazymonkyyy at gmail.com
Sat Aug 24 17:56:21 UTC 2024


On Saturday, 24 August 2024 at 16:36:17 UTC, Manu wrote:
> alias x = s.tupleof; // this works
> alias y = s.tupleof[0]; // this is a compile error?!
>
> How can embarrassing edge cases like this still exist in the 
> language today? I'm just reacquainting myself with all the 
> reasons that I've had such a hard time trying to convince my 
> colleagues to take D seriously for such a long time.
>
> 😫

```d
auto tuple(T...)(T args){
	struct Tuple{
		enum istuple=true;
		T me; alias me this;
	}
	return Tuple(args);
}
unittest{
	auto foo=tuple(1,"hi");
	assert(foo[0]==1);
	assert(foo[1]=="hi");
	auto bar=tuple();
}
auto totuple(T)(T a) if(is(typeof(a.istuple)))=>a;
auto totuple(T)(T a) if( ! is(typeof(a.istuple)))=>tuple(a);
auto maybetuple(T...)(T a){
	static if(T.length==1){
		return a[0];
	} else {
		return tuple(a);
}}
```

Could be fixed with a better std.meta


More information about the Digitalmars-d mailing list