Discussion:
No TypeTuple expansion for assert?
Ali Çehreli via Digitalmars-d
2014-10-03 19:08:19 UTC
Permalink
I know that assert is not a function but it would be nice to have.

import std.exception;

void foo(T...)(T args)
{
// Compiles:
enforce(args);

// DOES NOT COMPILE:
// assert(args);

// Must expand manually:
assert(args[0], args[1]);
}

void main()
{
foo(true, "hi");
}

Ali
Dmitry Olshansky via Digitalmars-d
2014-10-03 19:20:50 UTC
Permalink
Post by Ali Çehreli via Digitalmars-d
I know that assert is not a function but it would be nice to have.
Indeed. If we make it a function and put in object.d would anyone notice
the change?
Post by Ali Çehreli via Digitalmars-d
import std.exception;
void foo(T...)(T args)
{
enforce(args);
// assert(args);
assert(args[0], args[1]);
}
void main()
{
foo(true, "hi");
}
Ali
--
Dmitry Olshansky
monarch_dodra via Digitalmars-d
2014-10-03 20:02:14 UTC
Permalink
Post by Dmitry Olshansky via Digitalmars-d
Post by Ali Çehreli via Digitalmars-d
I know that assert is not a function but it would be nice to
have.
Indeed. If we make it a function and put in object.d would
anyone notice the change?
I think there are semantics that prevent that. Such as
"assert(0)", or removing the evaluation of arg in "assert(arg())"
altogether in release.
H. S. Teoh via Digitalmars-d
2014-10-03 20:26:25 UTC
Permalink
Post by Dmitry Olshansky via Digitalmars-d
Post by Ali Çehreli via Digitalmars-d
I know that assert is not a function but it would be nice to have.
Indeed. If we make it a function and put in object.d would anyone
notice the change?
I think there are semantics that prevent that. Such as "assert(0)", or
removing the evaluation of arg in "assert(arg())" altogether in
release.
void assert(lazy bool exp) {
version(assert) // (!)
if (!exp) __fatal_runtime_error();
}

Doesn't work for assert(0); but does work for not evaluating the
argument in release build. (Yeah I know, implementation of 'lazy' leaves
a lot to be desired, but hey, that can be argued to be a QOI issue.)


T
--
This is not a sentence.
monarch_dodra via Digitalmars-d
2014-10-04 19:58:16 UTC
Permalink
On Friday, 3 October 2014 at 20:28:21 UTC, H. S. Teoh via
On Fri, Oct 03, 2014 at 08:02:14PM +0000, monarch_dodra via
On Friday, 3 October 2014 at 19:21:38 UTC, Dmitry Olshansky
Post by Dmitry Olshansky via Digitalmars-d
Post by Ali Çehreli via Digitalmars-d
I know that assert is not a function but it would be nice to have.
Indeed. If we make it a function and put in object.d would
anyone
notice the change?
I think there are semantics that prevent that. Such as
"assert(0)", or
removing the evaluation of arg in "assert(arg())" altogether in
release.
void assert(lazy bool exp) {
version(assert) // (!)
if (!exp) __fatal_runtime_error();
}
Doesn't work for assert(0); but does work for not evaluating the
argument in release build. (Yeah I know, implementation of
'lazy' leaves
a lot to be desired, but hey, that can be argued to be a QOI
issue.)
T
There might still be an issue in regards to linking though: I
have some code where assert-only functions are only defined for
non-release.

Timon Gehr via Digitalmars-d
2014-10-04 16:07:59 UTC
Permalink
Post by Ali Çehreli via Digitalmars-d
I know that assert is not a function but it would be nice to have.
import std.exception;
void foo(T...)(T args)
{
enforce(args);
// assert(args);
assert(args[0], args[1]);
}
void main()
{
foo(true, "hi");
}
Ali
It should just auto-expand. There is no reason for it not to. Same for
static assert.
Loading...