create fixed length string of characters

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Aug 16 17:15:20 UTC 2024


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





More information about the Digitalmars-d-learn mailing list