Jump to page: 1 2
Thread overview
create fixed length string of characters
Aug 16
Bruce
Aug 16
IchorDev
Aug 16
IchorDev
Aug 17
Bruce
Aug 17
IchorDev
August 16

Is there an easy way to create a 60 character string in D?
Like in Python...
ul = '-'*60

August 16

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 = '-'*60

You 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);

}

August 16

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 = '-'*60

2 ways:

// 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.

August 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:

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.

August 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:

>

On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:

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.

August 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:
> > 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

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.

August 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:
> > 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

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:

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.

Oh, that’s awesome I didn’t know pure stuff can do that.

>

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.

August 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:

>

On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:

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.

I found the docs of dup here: https://dlang.org/library/std/container/array/array.html

But not idup. Where does that come from?

« First   ‹ Prev
1 2