Parameters declared as the alias of a template won't accept the arguments of the same type.
Tejas
notrealemail at gmail.com
Tue May 3 03:04:57 UTC 2022
On Tuesday, 3 May 2022 at 00:38:34 UTC, Tejas wrote:
> On Monday, 2 May 2022 at 22:01:51 UTC, ag0aep6g wrote:
>> On 02.05.22 22:47, Stanislav Blinov wrote:
>>> On Monday, 2 May 2022 at 20:16:04 UTC, ag0aep6g wrote:
>>>> On 02.05.22 21:17, Stanislav Blinov wrote:
>>>>> On Monday, 2 May 2022 at 16:29:05 UTC, Loara wrote:
>>>> [...]
>>>>>> ```d
>>>>>> template MyAlias(T){
>>>>>> alias MyAlias = int;
>>>>>> }
>>>>>>
>>>>>> T simp(T)(MyAlias!T val){
>>>>>> return T.init;
>>>>>> }
>>>>>>
>>>>>> int main(){
>>>>>> simp(3);//Impossible to deduce T
>> [...]
>>> That's not my answer. And it is nonsense, because my answer
>>> is - what T is irrelevant, MyAlias maps *all* types to int.
>>> Therefore `simp` takes an int, which is what the caller is
>>> passing.
>>
>> And what does it return?
>
> Perhaps one way could be to assume that the parameter passed is
> of type `aliasTemplate!typeOfVar` and see if it helps make
> sense of the invocation? This would only be done if the
> parameter's type doesn't directly match the required type nor
> it is implicitly convertible to it(which can be further
> constrained by... constraints...)
> For example, here:
> ```d
> template MyAlias(T){
> alias MyAlias = int;
> }
> // Please note that I changed code, since this is the only
> way to get the type with which the template parameter was
> initialized
> U simp(T:MyAlias!U, U)(auto ref T val){
> return U.init;
> }
>
> int main(){
> simp(3); // val is inferred to be of type MyAlias!int
> simp(”4”); // val is of type MyAlias!string
> simp!string([4,0]); val is of type MyAlias!(int[])
> MyAlias!float d =45;
> simp(d); // val is inferred to be of type
> MyAlias!(MyAlias!float) // hopefully though the second MyAlias
> doesn't have to exist and it can just assume the type to be int
> }
> ```
Edit: correcting the code
```d
template MyAlias(T){
alias MyAlias = int;
}
// Please note that I changed code, since this is the only
way to get the type with which the template parameter was
initialized
U simp(T:MyAlias!U, U)(auto ref T val){
return U.init;
}
int main(){
simp(3); // val is inferred to be of type MyAlias!int
simp(”4”); // val is of type MyAlias!string
simp([4,0]);// val is of type MyAlias!(int[])
MyAlias!float d =45;
simp(d); // val is inferred to be of type
MyAlias!(MyAlias!float) // hopefully though the second MyAlias
doesn't have to exist and it can just assume the type to be int
}
More information about the Digitalmars-d-learn
mailing list