Is there an easy way to create a 60 character string in D?
Like in Python...
ul = '-'*60
Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 16 create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
August 16 Re: create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bruce | 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? You can use the repeat() function in std.range to create a range of N consecutive elements. import std.range; void main()
} |
August 16 Re: create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bruce | 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? 2 ways:
I think the 2nd way may be more efficient, but it requires an unsafe cast. |
August 16 Re: create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Treleaven | 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:
I think you meant |
August 16 Re: create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
Posted in reply to IchorDev | 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: >On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
I think you meant
https://dlang.org/spec/function.html#pure-factory-functions So perhaps |
August 16 Re: create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Treleaven | 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:
> > 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.
>
> `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.
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 Davis
|
August 16 Re: create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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, 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 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 |
August 16 Re: create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Treleaven | 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:
> > 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.
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 Davis
|
August 16 Re: create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Treleaven | 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: >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:
I think you meant
Oh, that’s awesome I didn’t know pure stuff can do that. >https://dlang.org/spec/function.html#pure-factory-functions So perhaps It’s more explicit so I think still generally preferable. |
August 16 Re: create fixed length string of characters | ||||
---|---|---|---|---|
| ||||
Posted in reply to IchorDev | 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: >On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
I think you meant I found the docs of But not |