Why doesn't string implicitly convert to immutable(ubyte)[] in @safe mode?
Thread overview | |||||
---|---|---|---|---|---|
|
March 23, 2024 Implicit conversion of string to array of immutable ubytes | ||||
---|---|---|---|---|
| ||||
March 23, 2024 Re: Implicit conversion of string to array of immutable ubytes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On Saturday, March 23, 2024 12:11:15 AM MDT Per Nordlöw via Digitalmars-d- learn wrote:
> Why doesn't string implicitly convert to immutable(ubyte)[] in
> @safe mode?
Why would it? They're different types. Their elements happen to have the same size, but that doesn't mean that they're used for the same thing at all. And having them be implicitly convertible could cause serious problems with overloading.
If you want to do that conversion without a cast, then you can just use std.string.representation (which will do the cast internally).
- Jonathan M Davis
|
6 days ago Re: Implicit conversion of string to array of immutable ubytes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Saturday, 23 March 2024 at 06:55:41 UTC, Jonathan M Davis wrote:
> If you want to do that conversion without a cast, then you can just use std.string.representation (which will do the cast internally).
I somehow missed this in Programming in D, and even here on the forum. So just noting that if your code is straddling the worlds of ubyte[] and string, "representation" from std.string will give you the immutable ubyte[] (or ushort[]/uint[] as appropriate) for a string which you want to feed into a ubyte[] based API.
Andy
|
Copyright © 1999-2021 by the D Language Foundation