Re: Mutability issue

2024-03-24 Thread Menjanahary R. R. via Digitalmars-d-learn
On Saturday, 23 March 2024 at 20:49:14 UTC, Nick Treleaven wrote: On Saturday, 23 March 2024 at 19:30:29 UTC, Menjanahary R. R. wrote: for (T candidate = T(5); candidate * candidate <= n; candidate += T(6)) { When T is `const int`, the above code declares and initializes a constant

Re: Mutability issue

2024-03-24 Thread Menjanahary R. R. via Digitalmars-d-learn
On Saturday, 23 March 2024 at 20:38:40 UTC, Jonathan M Davis wrote: On Saturday, March 23, 2024 1:30:29 PM MDT Menjanahary R. R. via Digitalmars- d-learn wrote: [...] Well, when nextPrime is instantiated, the type of T is inferred from the function argument. So, if num is int, then T is int,

Re: Mutability issue

2024-03-23 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 23 March 2024 at 19:30:29 UTC, Menjanahary R. R. wrote: for (T candidate = T(5); candidate * candidate <= n; candidate += T(6)) { When T is `const int`, the above code declares and initializes a constant variable: ```d const int candidate = const int(5); ``` Then, at the

Re: Mutability issue

2024-03-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, March 23, 2024 1:30:29 PM MDT Menjanahary R. R. via Digitalmars- d-learn wrote: > The next code works as is but ... > > ``` > import std.stdio; > import std.traits; > > bool isPrime(T)(T n) if (isIntegral!T) { > if (n <= T(3)) return n > T(1); > > if (n % T(2) == T(0) || n %