digitalmars.D.learn - Type Parameter Deduction
- Salih Dincer (29/29) May 10 2022 Hi All,
- frame (9/13) May 10 2022 What about something like this?
- Salih Dincer (3/24) May 10 2022 SDB@79
- =?UTF-8?Q?Ali_=c3=87ehreli?= (8/15) May 10 2022 What would you like to see instead? The compiler can reject code only by...
- Salih Dincer (21/25) May 10 2022 Here that is:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (5/8) May 10 2022 Of course, 'static assert' is better there because it will catch the
- frame (18/19) May 10 2022 That is your type protection here, a constraint.
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
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 79What 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
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:Not compiled, the compiler returns:However, the compiler catches other errors! How can I solve this problem? Thanks... SDB 79What 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)) { //... } ```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
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". :)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. AliInclusiveRange.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)`
May 10 2022
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:Thank you, I solved the problem by adding a second function. D, you are great! SDB 79must satisfy the following constraint:That is your type protection here, a constraint.
May 10 2022
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
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