digitalmars.D - concepts
- Vlad Levenfeld (17/17) May 21 2015 How possible is something like this:
- extrawurst (2/19) May 21 2015 that would be great!
- Dennis Ritchie (12/12) May 21 2015 Is it possible to write something like this in the compile time?
- ZombineDev (18/30) May 21 2015 Although the compiler already has enough information, this is not
- Dennis Ritchie (48/81) May 21 2015 Yes, but I can do that I need check use strings ( source is taken
- Dennis Ritchie (3/10) May 21 2015 Link wrong:
How possible is something like this: auto foo (T)(T t) if (is(Eq!T) && is(Num!T) && T.sizeof == 4) {...} foo ("hello"); then you get some output from the compiler, something like Error: template foo cannot deduce function from argument types !()(string), candidates are: foo(T)(T x) pass is(Eq!T) fail is(Num!T) fail T.sizeof == 4 Since template code (and by extension its constraint) is known to the compiler, would it break anything to have it parse the logical connectives and state specifically what fails for each attempted match?
May 21 2015
On Thursday, 21 May 2015 at 08:17:47 UTC, Vlad Levenfeld wrote:How possible is something like this: auto foo (T)(T t) if (is(Eq!T) && is(Num!T) && T.sizeof == 4) {...} foo ("hello"); then you get some output from the compiler, something like Error: template foo cannot deduce function from argument types !()(string), candidates are: foo(T)(T x) pass is(Eq!T) fail is(Num!T) fail T.sizeof == 4 Since template code (and by extension its constraint) is known to the compiler, would it break anything to have it parse the logical connectives and state specifically what fails for each attempted match?that would be great!
May 21 2015
Is it possible to write something like this in the compile time? auto foo(T)(T t) if (is(T == immutable(int)) && t == 5) // Error: variable t cannot be read at compile time { // ... } void main() { immutable(int) n = 5; foo(n); }
May 21 2015
On Thursday, 21 May 2015 at 11:05:10 UTC, Dennis Ritchie wrote:Is it possible to write something like this in the compile time? auto foo(T)(T t) if (is(T == immutable(int)) && t == 5) // Error: variable t cannot be read at compile time { // ... } void main() { immutable(int) n = 5; foo(n); }Although the compiler already has enough information, this is not possible because the template constraints are not allowed to use the runtime parameters (they are compile-time only constraints). It would be more doable like this: auto foo(T)(T t) if (is(T == immutable(int))) { assert(t == 5); } void main() { immutable int n = 6; foo(n); // Possible in the future: // dmd test.d --deep-asserts-verification // Error: function foo has an assert which can't satisfied: // assert(5 == n) <- n is 6 }
May 21 2015
On Thursday, 21 May 2015 at 11:16:47 UTC, ZombineDev wrote:On Thursday, 21 May 2015 at 11:05:10 UTC, Dennis Ritchie wrote:Yes, but I can do that I need check use strings ( source is taken away http://ddili.org/ders/d.en/mixin.html ): import std.conv : to; import std.string : split; import std.stdio : writeln; int[] filter(string predicate)(in int[] numbers) if (predicate.split[0] == "number" && predicate.split[1] == ">" && predicate.split[2].to!int <= int.max) { int[] result; foreach (number; numbers) { if (mixin (predicate)) { result ~= number; } } return result; } void main() { auto a = [1, 2, 3, 4, 5]; writeln(a.filter!"number > 3"); // [4, 5] } ----- http://rextester.com/LNTB90172 Code Ali is not safe, because I can refer to the inner filter function directly in a function call :) import std.conv : to; import std.string : split; import std.stdio : writeln; int[] filter(string predicate)(in int[] numbers) { int[] result; foreach (number; numbers) { if (mixin (predicate)) { result ~= number; } } return result; } void main() { auto a = [1, 2, 3, 4, 5]; writeln(a.filter!"result != [1, 2]"); // [1, 2] } ----- http://rextester.com/LNTB90172Is it possible to write something like this in the compile time? auto foo(T)(T t) if (is(T == immutable(int)) && t == 5) // Error: variable t cannot be read at compile time { // ... } void main() { immutable(int) n = 5; foo(n); }Although the compiler already has enough information, this is not possible because the template constraints are not allowed to use the runtime parameters (they are compile-time only constraints). It would be more doable like this: auto foo(T)(T t) if (is(T == immutable(int))) { assert(t == 5); } void main() { immutable int n = 6; foo(n); // Possible in the future: // dmd test.d --deep-asserts-verification // Error: function foo has an assert which can't satisfied: // assert(5 == n) <- n is 6 }
May 21 2015
On Thursday, 21 May 2015 at 13:25:56 UTC, Dennis Ritchie wrote:void main() { auto a = [1, 2, 3, 4, 5]; writeln(a.filter!"result != [1, 2]"); // [1, 2] } ----- http://rextester.com/LNTB90172Link wrong: http://rextester.com/UPUB47730
May 21 2015