June 18, 2020
On Thursday, 18 June 2020 at 12:50:35 UTC, Stanislav Blinov wrote:
> No, there isn't a way to write an operator.

OK, first choice eliminated.

On Thursday, 18 June 2020 at 13:57:39 UTC, Dukc wrote:
> No reason to use templates here
>
> pragma(inline, true) auto not(bool cond) { return !cond(); }

I like it. The inline pragma eliminates the extra overhead of the function call, which was another objective. (And it introduces me to D's pragmas too.)

Personally, I find this:

  if ( not( abra && cadabra )) ...

to be more clear than:

  if ( !( abra && cadabra )) ...

I guess it depends on what one is used to. I do recognize that this would be non-standard for D, but I'm still going to use it because I find it more readabile.

I should add that this one made me laugh though, giving flashbacks to that horrible "not speak" of the early 90s:

  if ( configfile.isFile.not ) ...

LOL

I've learned multiple things from this posting. Thank you all for sharing your suggestions.

Denis
June 18, 2020
On 6/18/20 5:13 AM, Denis wrote:

> Templates offer a clean syntax

Here is an earlier experiment of nested templates, which may be useful in this case. This is unrelated to your problem but the syntax can be pretty readable with templates:

// If there are template arguments, then the result is the first of
// them. If there is no argument, then the result is the argument
// of 'otherwise'.
template FirstOf(T...) {
  template otherwise(alias D) {
    static if (T.length != 0) {
      enum otherwise = T[0];

    } else {
      enum otherwise = D;
    }
  }
}

unittest {
  static assert (FirstOf!(1.5, "hello").otherwise!100 == 1.5);
  static assert (FirstOf!().otherwise!42 == 42);
}

auto foo(Args...)() {
  auto temp = FirstOf!Args.otherwise!1.5;
  // ...
  return temp + 0.5;
}

unittest {
  assert(foo!(10, int[])() == 10.5);
  assert(foo() == 2.0);
}

void main() {
}

I think you should be able to pass callables as 'alias' template arguments but I couldn't put much thought into it.

Ali

June 18, 2020
On Thursday, 18 June 2020 at 17:57:49 UTC, Ali Çehreli wrote:
> Here is an earlier experiment of nested templates, which may be useful in this case.
    :
> I think you should be able to pass callables as 'alias' template arguments

Sounds good. This gives me an opportunity to learn how nested templates can be used. `alias` in this context is also new, so I'll dig into that too.

Thanks!

June 19, 2020
On Thursday, 18 June 2020 at 17:39:44 UTC, Denis wrote:

> I should add that this one made me laugh though, giving flashbacks to that horrible "not speak" of the early 90s:
>
>   if ( configfile.isFile.not ) ...
>
> LOL

Approve Yoda does.
1 2
Next ›   Last »