May 30
On Tue, May 30, 2023 at 04:27:05PM -0700, Walter Bright via Digitalmars-d wrote:
> Excellent! The technique you're exhibiting is called "mocking". It works especially well when a function's inputs and outputs are properly parameterized.

Yes, it's an age-old technique since at least the days of Java.

What makes it stand out in this particular case, though, is D's ability to let you mock references to global (module-level) symbols with just a 1-line change.  To mock the top-level `System` in Java, for example, you have to do some arcane black magic with the help of an external package; it's not something for mere mortals or the faint of heart to do by hand. In D, you add a single template parameter to your function, and you're all set to go.  Now *that's* power.


> When I fixed the D lexer.d to not access any globals, I was able to simplify the unittests for it by mocking up the inputs and outputs. For example, instead of having to use the global "gagging" switch to suppress error messages, I used a mock error handler that just did nothing.

Good to hear.  Error gagging is the source of some of the most irritating D compiler bugs in the past; the less it gets used, the better IMNSHO.


T

-- 
Genius may have its limitations, but stupidity is not thus handicapped. -- Elbert Hubbard
May 30
On Wed, May 31, 2023 at 12:28:40AM +0000, Salih Dincer via Digitalmars-d wrote: [...]
> I didn't know that `while` is used just like `foreach`. 0k4y, we can use `auto` in `while` condition or `if`, which lives in the same block as `auto`.
> 
> ```d
> while (line; outputTemplate.byLine) {...}
> ```
> 
> Does the following really work, thanks?

Oops, that's supposed to be `foreach`, not `while`. :-/  My bad.


T

-- 
Trying to define yourself is like trying to bite your own teeth. -- Alan Watts