On Monday, 29 March 2021 at 01:39:16 UTC, Adam D. Ruppe wrote:
On Monday, 29 March 2021 at 01:24:13 UTC, Preetpal wrote:
    writeln(isRandomAccessRange(arr));

Template arguments are passed by !(), not just ().

I believe you must also pass `typeof(arr)` since isRandomAccessRange is only interested in types.

Thanks. This answer led me in the right direction. What I was really trying to figure out was why I was not able to use the std.random library to take a random sampling from an instance of char[].


void main() {
    import std.random;
    import std.range;
    import std.stdio;
    import std.string;

    char[] arr = ['a', 'b', 'c'];
    int[] arr2 = [1, 2, 3];

    writeln(isRandomAccessRange!(typeof(arr)));
    writeln(typeid(10.iota));
    writeln(typeid(arr[0..$]));

    writeln(hasLength!(typeof(arr)));
    writeln(hasLength!(typeof(arr2)));

    auto rnd = Random(unpredictableSeed);

    auto randomnumbers = 3.iota.randomSample(2, rnd);

    writeln(randomnumbers);

// auto randomchars = arr.randomSample(2, rnd).array; // does not work auto randomchars = arr.representation.randomSample(2, rnd).array;

    writeln(cast(char[])randomchars);
}


I could not understand the error messages that the compiler was giving me. The fact that the compiler was providing candidates was leading me to believe that there was a type deduction issue (like as if it was not able to choose between candidates because of an ambiguity). This led me to not pay attention to the most important part of the the error message related to the type constraints.

mini_example.d(19): Error: template std.random.randomSample cannot deduce function from argument types !()(char[], int, MersenneTwisterEngine!(uint, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u)), candidates are: b:\D\DMD\D\dmd2\windows\bin\..\..\src\phobos\std\random.d(3858): randomSample(Range)(Range r, size_t n, size_t total) b:\D\DMD\D\dmd2\windows\bin\..\..\src\phobos\std\random.d(3865): randomSample(Range)(Range r, size_t n) b:\D\DMD\D\dmd2\windows\bin\..\..\src\phobos\std\random.d(3872): randomSample(Range, UniformRNG)(Range r, size_t n, size_t total, auto ref UniformRNG rng) b:\D\DMD\D\dmd2\windows\bin\..\..\src\phobos\std\random.d(3879): randomSample(Range, UniformRNG)(Range r, size_t n, auto ref UniformRNG rng)
  with Range = char[],
UniformRNG = MersenneTwisterEngine!(uint, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u)
  must satisfy the following constraint:
       hasLength!Range

I did not realize that the compiler was telling me that the range that I was providing did not satisfy that constraint. I only started to try testing the type constraints as a sanity check (which was when I ran into this error).

Reply via email to