August 15
On Tue, Aug 15, 2023 at 10:25:56AM +0000, sighoya via Digitalmars-d wrote:
> On Monday, 14 August 2023 at 14:49:06 UTC, Paul Backus wrote:
[...]
> > ```d
> > template square(T)
> > if (is(T)) // <-- right here
> > {
> >     T square(T param) { return param*param; }
> > }
> > ```
> 
> Can you tell what `is(T)` is doing here?
> From the docs it seems to require `T` to be existing.
> However, I don't know what this exactly means, as T anyway have to exist.

`is(T)` is standard idiom for checking whether T is a valid type.

It's usually used with an expression to test whether some operation is valid on a type, e.g.:

	auto myTemplateFunc(T)(T data)
		if (is(data++))	// make sure ++ is valid on T
	{
		return data++;
	}

	auto myTemplateFunc(T)(T data)
		if (is(data+0))	// make sure + int is valid on T
	{
		return data + 123;
	}


T

-- 
English is useful because it is a mess. Since English is a mess, it maps well onto the problem space, which is also a mess, which we call reality. Similarly, Perl was designed to be a mess, though in the nicest of all possible ways. -- Larry Wall
August 15

On Tuesday, 15 August 2023 at 10:25:56 UTC, sighoya wrote:

>

Can you tell what is(T) is doing here?
From the docs it seems to require T to be existing.
However, I don't know what this exactly means, as T anyway have to exist.

It doesn't do anything. It's only there because I copied it from Michael Galuza's post (where it also doesn't do anything): https://forum.dlang.org/post/qglavaqzfzzxjcrfufpw@forum.dlang.org

August 15
On Tuesday, 15 August 2023 at 10:54:52 UTC, H. S. Teoh wrote:
 `is(T)` is standard idiom for checking whether T is a valid
> type.
>
> It's usually used with an expression to test whether some operation is valid on a type, e.g.:
>
> 	auto myTemplateFunc(T)(T data)
> 		if (is(data++))	// make sure ++ is valid on T
> 	{
> 		return data++;
> 	}
>
> 	auto myTemplateFunc(T)(T data)
> 		if (is(data+0))	// make sure + int is valid on T
> 	{
> 		return data + 123;
> 	}

Those should be `is(typeof(data++))` and `is(typeof(data+0))`.
August 15
On Tuesday, 15 August 2023 at 10:54:52 UTC, H. S. Teoh wrote:
>
> `is(T)` is standard idiom for checking whether T is a valid type.

Thanks.

Though, is there any case where it wouldn't be a valid type?


August 15
On Tuesday, 15 August 2023 at 21:24:34 UTC, sighoya wrote:
> On Tuesday, 15 August 2023 at 10:54:52 UTC, H. S. Teoh wrote:
>>
>> `is(T)` is standard idiom for checking whether T is a valid type.
>
> Thanks.
>
> Though, is there any case where it wouldn't be a valid type?

As template parameter types seem to be distinguishable by syntax in my eyes:
https://dlang.org/spec/template.html#parameters
August 16
On Tuesday, 15 August 2023 at 21:24:34 UTC, sighoya wrote:
> On Tuesday, 15 August 2023 at 10:54:52 UTC, H. S. Teoh wrote:
>>
>> `is(T)` is standard idiom for checking whether T is a valid type.
>
> Thanks.
>
> Though, is there any case where it wouldn't be a valid type?

See:
https://dlang.org/spec/expression.html#is-type
1 2
Next ›   Last »