Skip to content

Commit 26afdfa

Browse files
committed
Adapt code to support -preview=in
When the switch is provided, '[auto] ref in' becomes illegal in the parser. This little trick, using static if + mixin, allow to have a different function based on whether the switch is used or not.
1 parent 99977c0 commit 26afdfa

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

source/mir/ndslice/internal.d

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,24 @@ template DynamicArrayDimensionsCount(T)
259259
enum size_t DynamicArrayDimensionsCount = 0;
260260
}
261261

262-
bool isPermutation(size_t N)(auto ref in size_t[N] perm)
262+
// -preview=in enabled, no need for `auto ref`
263+
static if (!is(typeof(mixin(q{(in ref int a) => a}))))
263264
{
264-
int[N] mask;
265-
return isValidPartialPermutationImpl(perm, mask);
265+
bool isPermutation(size_t N)(in size_t[N] perm)
266+
{
267+
int[N] mask;
268+
return isValidPartialPermutationImpl(perm, mask);
269+
}
270+
}
271+
else // -preview=in disabled
272+
{
273+
mixin(q{
274+
bool isPermutation(size_t N)(auto ref in size_t[N] perm)
275+
{
276+
int[N] mask;
277+
return isValidPartialPermutationImpl(perm, mask);
278+
}
279+
});
266280
}
267281

268282
version(mir_test) unittest
@@ -328,12 +342,28 @@ private enum isReference(P) =
328342
alias ImplicitlyUnqual(T) = Select!(isImplicitlyConvertible!(T, Unqual!T), Unqual!T, T);
329343
alias ImplicitlyUnqual(T : T*) = T*;
330344

331-
size_t lengthsProduct(size_t N)(auto ref in size_t[N] lengths)
345+
// -preview=in is enabled
346+
static if (!is(typeof(mixin(q{(in ref int a) => a}))))
332347
{
333-
size_t length = lengths[0];
334-
foreach (i; Iota!(1, N))
348+
size_t lengthsProduct(size_t N)(in size_t[N] lengths)
349+
{
350+
size_t length = lengths[0];
351+
foreach (i; Iota!(1, N))
335352
length *= lengths[i];
336-
return length;
353+
return length;
354+
}
355+
}
356+
else
357+
{
358+
mixin(q{
359+
size_t lengthsProduct(size_t N)(auto ref in size_t[N] lengths)
360+
{
361+
size_t length = lengths[0];
362+
foreach (i; Iota!(1, N))
363+
length *= lengths[i];
364+
return length;
365+
}
366+
});
337367
}
338368

339369
pure nothrow version(mir_test) unittest

0 commit comments

Comments
 (0)