Re: @safe question
On Monday, 10 January 2022 at 01:16:31 UTC, forkit wrote: On Sunday, 9 January 2022 at 21:56:05 UTC, Salih Dincer wrote: Try the @trusted and in/out: ... .. . thanks for introducing me to the in/out feature of D :-) I'll certainly look into that feature more. But my question still remains: //pointers ~= // why is this *not* allowed in @safe pointers ~= [i]; // while this *is* allowed in @safe Taking the address of a local variable is forbidden in @safe code. Even though str is a ref variable that points to a heap-allocated string, it is still considered a local variable because it is declared inside the body of a function.
Re: @safe question
On Sunday, 9 January 2022 at 21:56:05 UTC, Salih Dincer wrote: Try the @trusted and in/out: ... .. . thanks for introducing me to the in/out feature of D :-) I'll certainly look into that feature more. But my question still remains: //pointers ~= // why is this *not* allowed in @safe pointers ~= [i]; // while this *is* allowed in @safe
Re: @safe question
On Sunday, 9 January 2022 at 20:58:05 UTC, forkit wrote: Do not understand why one line is not considered @safe, but the other is. // module test; import std; @safe void main() { immutable string[] strings = ["one", "one", "two"]; immutable(string)*[] pointers = null; foreach(size_t i, ref str; strings) { if(str == "one") { //pointers ~= // not allowed in @safe ?? pointers ~= [i]; // for @safe, I have to revert to using an index into strings. } i++; } } //- Try the @trusted and in/out: ```d auto pro(in immutable string[] strings, out immutable(string)*[] pointers) @trusted { foreach(i, ref str; strings) { if(str == "one") { //pointers ~= [i]/* ok pointers ~= //*/ } /* unnecessary: i++;//*/ } } @safe void main() { immutable string[] strings = ["one", "one", "two"]; immutable(string)*[] pointers = null; strings.pro(pointers); assert(pointers[0] == [0]); // ok assert(pointers[1] == [1]); // ok } ```
@safe question
Do not understand why one line is not considered @safe, but the other is. // module test; import std; @safe void main() { immutable string[] strings = ["one", "one", "two"]; immutable(string)*[] pointers = null; foreach(size_t i, ref str; strings) { if(str == "one") { //pointers ~= // not allowed in @safe ?? pointers ~= [i]; // for @safe, I have to revert to using an index into strings. } i++; } } //-
Re: srand in D
On Sunday, 9 January 2022 at 03:15:02 UTC, Ali Çehreli wrote: What would work in the code above is 'choice': char q = choice(allowed_chars); But that hits another fact of D: arrays of chars are strings, which cannot be RandomAccessRange because individual chars must be decoded to form dchars. One can use explicit index notation which is nearly as compact as the template parameter form: string salt = iota (0, 16) .map!(i => saltchars [uniform(0, $)]) .array;