digitalmars.D.learn - create fixed length string of characters
- Bruce (3/3) Aug 15 Is there an easy way to create a 60 character string in D?
- Olivier Pisano (12/15) Aug 15 You can use the repeat() function in std.range to create a range
- Nick Treleaven (21/24) Aug 16 2 ways:
- IchorDev (2/8) Aug 16 I think you meant `idup`? `dup` will make it mutable.
- Nick Treleaven (7/15) Aug 16 `dup` actually works. I tested the code before posting. It should
- Jonathan M Davis (11/27) Aug 16 Well, you if you use dup, you're asking for a mutable array, whereas if ...
- Nick Treleaven (7/17) Aug 16 Yes, `idup` may be needed e.g. for overloads varying on
- Jonathan M Davis (15/21) Aug 16 Yes, but if you use idup, then the result is always immutable, whereas i...
- IchorDev (3/20) Aug 16 It’s more explicit so I think still generally preferable.
- Renato Athaydes (4/12) Aug 16 I found the docs of `dup` here:
- Renato Athaydes (5/19) Aug 16 Ah nevermind, I was looking at the container `Array` not the
- Bruce (11/17) Aug 16 This seems to work without having to make a string dup. I just
- IchorDev (3/5) Aug 17 In this case it’s fine to.
Is there an easy way to create a 60 character string in D? Like in Python... ul = '-'*60
Aug 15
On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:Is there an easy way to create a 60 character string in D? Like in Python... ul = '-'*60You can use the repeat() function in std.range to create a range of N consecutive elements. If you need to assign this range to a string, you'll have to use the array() function. import std.range; import std.stdio; void main() { string s = '-'.repeat(60).array; writeln(s); }
Aug 15
On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:Is there an easy way to create a 60 character string in D? Like in Python... ul = '-'*602 ways: ```d // use a fixed array: immutable char[60] a = '-'; string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); // allocate mutable array on heap char[] b = new char[60]; b[] = '-'; // if b is unique and no longer used s = cast(string)b; // not safe s.writeln(); ``` `a` is initialized from a single element which is copied over each of its elements. `b` uses an array operation to assign a single element to each of its elements. I think the 2nd way may be more efficient, but it requires an unsafe cast.
Aug 16
On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); ```I think you meant `idup`? `dup` will make it mutable.
Aug 16
On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:`dup` actually works. I tested the code before posting. It should be that `dup` is a template function which gets inferred as strongly `pure`, so the result is known to the compiler to be unique. https://dlang.org/spec/function.html#pure-factory-functions So perhaps `idup` is no longer needed.On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); ```I think you meant `idup`? `dup` will make it mutable.
Aug 16
On Friday, August 16, 2024 9:58:31 AM MDT Nick Treleaven via Digitalmars-d- learn wrote:On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:Well, you if you use dup, you're asking for a mutable array, whereas if you use idup, you're asking for an immutable array. Whether the result of dup is then able to be implicitly converted to immutable based on whether the operation is pure depends on the element type and where the result is used. So, dup may very well work in this particular case, but in the general case, you really want to be using idup if you want immutable. And in this particular example, all it would take to make the result not immutable would be to use auto instead of string. - Jonathan M DavisOn Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:`dup` actually works. I tested the code before posting. It should be that `dup` is a template function which gets inferred as strongly `pure`, so the result is known to the compiler to be unique. https://dlang.org/spec/function.html#pure-factory-functions So perhaps `idup` is no longer needed.On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); ```I think you meant `idup`? `dup` will make it mutable.
Aug 16
On Friday, 16 August 2024 at 16:30:09 UTC, Jonathan M Davis wrote:Well, you if you use dup, you're asking for a mutable array, whereas if you use idup, you're asking for an immutable array.Yes, `idup` may be needed e.g. for overloads varying on mutability.Whether the result of dup is then able to be implicitly converted to immutable based on whether the operation is pure depends on the element type and where the result is used.If it can't be converted to immutable then `idup` won't work either.So, dup may very well work in this particular case, but in the general case, you really want to be using idup if you want immutable. And in this particular example, all it would take to make the result not immutable would be to use auto instead of string.Because `idup` exists, sure use that. But writing `immutable s = a.dup;` does the same thing.
Aug 16
On Friday, August 16, 2024 10:37:45 AM MDT Nick Treleaven via Digitalmars-d- learn wrote:On Friday, 16 August 2024 at 16:30:09 UTC, Jonathan M Davis wrote:Yes, but if you use idup, then the result is always immutable, whereas if you use dup, it's mutable unless it's used in a context where it has to be immutable. So, in the general case, you need to use idup if you want to be sure that you get immutable. You can still get immutable in specific cases, but there will be plenty of cases where you won't, and even if you do right now, that could change when refactoring. Something as simple as a function being changed from taking string to taking a range of characters could then change the type that you get. So, it's probably better practice to just always use idup when you want immutable, but obviously, developers can choose to do otherwise and make it work just fine so long as they're consistently using the result of dup in a context which requires immutable. - Jonathan M DavisWhether the result of dup is then able to be implicitly converted to immutable based on whether the operation is pure depends on the element type and where the result is used.If it can't be converted to immutable then `idup` won't work either.
Aug 16
On Friday, 16 August 2024 at 15:58:31 UTC, Nick Treleaven wrote:On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:Oh, that’s awesome I didn’t know pure stuff can do that.On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:`dup` actually works. I tested the code before posting. It should be that `dup` is a template function which gets inferred as strongly `pure`, so the result is known to the compiler to be unique.On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); ```I think you meant `idup`? `dup` will make it mutable.https://dlang.org/spec/function.html#pure-factory-functions So perhaps `idup` is no longer needed.It’s more explicit so I think still generally preferable.
Aug 16
On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:I found the docs of `dup` here: https://dlang.org/library/std/container/array/array.html But not `idup`. Where does that come from?On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); ```I think you meant `idup`? `dup` will make it mutable.
Aug 16
On Friday, 16 August 2024 at 18:00:25 UTC, Renato Athaydes wrote:On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote:Ah nevermind, I was looking at the container `Array` not the built-in array type, which is documented here: https://dlang.org/spec/arrays.html#array-properties which does mention `idup`!On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:I found the docs of `dup` here: https://dlang.org/library/std/container/array/array.html But not `idup`. Where does that come from?On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); ```I think you meant `idup`? `dup` will make it mutable.
Aug 16
On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote:```d // use a fixed array: immutable char[60] a = '-'; string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln();This seems to work without having to make a string dup. I just wanted to write a separator line in my output. Is there anything wrong with using the char[60] directly? import std.stdio; immutable char[60] a = '-'; void main() { a.writeln(); }
Aug 16
On Saturday, 17 August 2024 at 06:03:09 UTC, Bruce wrote: Is there anything wrongwith using the char[60] directly?In this case it’s fine to.
Aug 17