TypeFunction example: ImplictConvTargets
claptrap
clap at trap.com
Tue Oct 6 23:16:47 UTC 2020
On Tuesday, 6 October 2020 at 18:00:29 UTC, Daniel K wrote:
> On Monday, 5 October 2020 at 11:44:34 UTC, Stefan Koch wrote:
>
> template ImplicitConversionTargets(T)
> {
> alias ImplicitConversionTargets = ImplicitConvertible!(T,
> basic_types);
> }
>
> pragma (msg, ImplicitConversionTargets!(long));
>
> That's 16 lines of code. Heck it even compiles in D1 if only
> the alias declarations are written in the old style.
>
> Personally I prefer using existing language features.
>
> /Daniel K
If someone said to you "I have this list of integers and I want
to get a new list containing the ones are divisible by a specific
value", would you write this...
----
int[] vals = [4,7,28,23,585,73,12];
int[] getMultiplesX(int i, int candidate, int[] candidates)
{
int[] result;
if ((candidate % i) == 0)
result ~= candidate;
if (candidates.length > 0)
return result ~ getMultiplesX(i, candidates[0],
candidates[1..$]);
else
return result;
}
int[] getMultiplesOf(int i)
{
return getMultiplesX(i, vals[0], vals[1..$]);
}
----
Or would you write it like this...
int[] vals = [4,7,28,23,585,73,12];
int[] getMultiplesOf(int i)
{
int[] result;
foreach(v; vals)
if ((v % i) == 0) result ~= v;
return result;
}
----
Its the same basic algorithm, search a list and make a new list
from the entries that satisfy a certain condition. It beggars
belief that anyone would argue that the second version is not
better on every metric that is important in writing good code.
It's clearer, more intuitive, more concise, it will almost
definitely be faster and less wasteful of resources. Newbies will
grep it far quicker than the other versions. That means faster to
write and easier to maintain and debug.
I'm honestly losing the will to live here.
More information about the Digitalmars-d
mailing list