www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Type Parameter Deduction

reply Salih Dincer <salihdb hotmail.com> writes:
Hi All,

The problem is that I want to disable the use of boolean type. 
But this is not possible with contract programming or otherwise. 
Actually, it's possible to fix it with contract programming or 
otherwise throwing errors.

```d
alias ir = inclusiveRange;
auto inclusiveRange(T = int)(T f = T(0), T l = T(0), T s = T(1))
in(!is(T == bool), "Boolean type cannot be used!") {/*
{
   static if(is(T == bool))
   {
     import std.format;
   	assert(false, format("This type (%s) cannot be used!", 
typeid(T)));
   }//*/
   if(!l) {
     l = f;
     f = 0;
   }
   return InclusiveRange!T(f, l, s);
} /* Error:
    * operation not allowed on `bool` `this.last -= this.step`
    */
```
However, the compiler catches other errors! How can I solve this 
problem?

Thanks...

SDB 79
May 10 2022
parent reply frame <frame86 live.com> writes:
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)) { //... } ```
May 10 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
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
May 10 2022
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
May 10 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
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
May 10 2022
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
May 10 2022
prev sibling parent frame <frame86 live.com> writes:
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 ```
May 10 2022