Re: Type Parameter Deduction

2022-05-10 Thread Ali Çehreli via Digitalmars-d-learn

On 5/10/22 08:40, Salih Dincer wrote:

> void inclusiveRange(T)(T Args...) if(is(T == bool)) {
>assert(0, "\nBoolean type cannot be used!");
> }

Of course, 'static assert' is better there because it will catch the 
issue at compile time.


static assert(0, "\nBoolean type cannot be used!");

Ali



Re: Type Parameter Deduction

2022-05-10 Thread Salih Dincer via Digitalmars-d-learn

On Tuesday, 10 May 2022 at 14:42:31 UTC, Ali Çehreli wrote:

What would you like to see instead? [...]


Here that is:

```d
void inclusiveRange(T)(T Args...) if(is(T == bool)) {
  assert(0, "\nBoolean type cannot be used!");
}/*
core.exception.AssertError@InclusiveRangev2.d(79):
Boolean type cannot be used!

??:? _d_assert_msg [0x55e17c3c881e]
??:? pure nothrow @safe void 
source.inclusiveRange!(bool).inclusiveRange(bool...) 
[0x55e17c3c575f]

??:? _Dmain [0x55e17c3a8e43]
Process finished with exit code 1.
*/
```

Tuesday, 10 May 2022 at 14:44:06 UTC, frame wrote:

On Tuesday, 10 May 2022 at 13:14:20 UTC, Salih Dincer wrote:


must satisfy the following constraint:


That is your type protection here, a constraint.


Thank you, I solved the problem by adding a second function.

D, you are great!

SDB@79


Re: Type Parameter Deduction

2022-05-10 Thread frame via Digitalmars-d-learn

On Tuesday, 10 May 2022 at 13:14:20 UTC, Salih Dincer wrote:


must satisfy the following constraint:


That is your type protection here, a constraint.

Alternatively you can put the constraint in a function and make 
it more verbose:


```d
bool isAllowedType(T)()
{
static assert(!is(T == bool), "Nope");
return true;
}

void fun(T = int)(T arg)
if(isAllowedType!T)
{
//..
}

fun(0);  // ok
fun(false);  // not allowed
```


Re: Type Parameter Deduction

2022-05-10 Thread Ali Çehreli via Digitalmars-d-learn

On 5/10/22 06:14, Salih Dincer wrote:

> Not compiled, the compiler returns:

What would you like to see instead? The compiler can reject code only by 
"not compiling". :)


>> InclusiveRange.d(48): Error: template instance
>> `InclusiveRange.ir!bool` does not match > template declaration
>> `inclusiveRange(T = int)(T f = T(0), T l = T(0), T s = T(1))`
>>   with `T = bool`
>>  must satisfy the following constraint:
>> `   !is(T == bool)`

It explains the problem by displaying the reason on the last line.

So, the assumption is that inclusiveRange's documentation makes it clear 
that it cannot be used with 'bool' and the user sees a compilation error 
if they try to do that.


Ali



Re: Type Parameter Deduction

2022-05-10 Thread Salih Dincer via Digitalmars-d-learn

On Tuesday, 10 May 2022 at 12:56:50 UTC, frame wrote:

On Tuesday, 10 May 2022 at 09:26:46 UTC, Salih Dincer wrote:

However, the compiler catches other errors! How can I solve 
this problem?


Thanks...

SDB@79


What about something like this?

```d
auto inclusiveRange(T = int)(T f = T(0), T l = T(0), T s = T(1))
if(!is(T == bool))
{
//...
}
```


Not compiled, the compiler returns:
InclusiveRange.d(48): Error: template instance 
`InclusiveRange.ir!bool` does not match > template declaration 
`inclusiveRange(T = int)(T f = T(0), T l = T(0), T s = T(1))`

  with `T = bool`
 must satisfy the following constraint:
`   !is(T == bool)`


SDB@79



Re: Type Parameter Deduction

2022-05-10 Thread frame via Digitalmars-d-learn

On Tuesday, 10 May 2022 at 09:26:46 UTC, Salih Dincer wrote:

However, the compiler catches other errors! How can I solve 
this problem?


Thanks...

SDB@79


What about something like this?

```d
auto inclusiveRange(T = int)(T f = T(0), T l = T(0), T s = T(1))
if(!is(T == bool))
{
//...
}
```