Error: `std.uni.isUpper` conflicts with `std.ascii.isUpper`
WebFreak001
d.forum at webfreak.org
Wed Jul 15 07:01:34 UTC 2020
On Tuesday, 14 July 2020 at 20:37:53 UTC, Marcone wrote:
> import std: isUpper, writeln;
>
> void main(){
> writeln(isUpper('A'));
> }
>
> Why I get this error? How can I use isUpper()?
Additionally to the other answers telling you how to fix it, it's
important to know why it happens in the first place:
std.ascii defines an isUpper method which only works on ASCII
characters (byte value < 128), which means it's a really simple
and very fast check, basically the same as `c >= 'A' && c <= 'Z'`
but usually more readable. The other functions in std.ascii work
the same and are all just very convenient methods for these
simple checks. You should only really use this for file formats
and internal strings, never for anything the user could input
(preferably not even for validation of English applications)
std.uni defines an isUpper method as defined in the unicode 6.2
spec (the whole std.uni module is for these standardized unicode
classifications and transformations, but NOT for UTF
encoding/decoding, that's what std.utf is for), which means it
works for a lot more languages than just the basic English
alphabet in ASCII, at the cost of being slower.
If you have a normal import like `import std;` you can specify
"more specified" imports using `import std.uni : isUpper;` or
`import std.ascii : isUpper;` which will not clash with your
first import, as long as you don't try to import both in the same
scope. Basically:
import std;
import std.uni : isUpper;
bool someUserInputTest(string x) { return x[0].isUpper; }
Note that none of D's std.uni functions support using any locale,
so if you want to do anything more than the default language
neutral unicode locale you will have to use some library or using
the OS APIs. Note also that D is missing some unicode properties
like some check for characters which aren't lowercase or
UPPERCASE but Titlecase (like Dz [U+01F2 LATIN CAPITAL LETTER D
WITH SMALL LETTER Z]) which you could however use OS APIs for.
More information about the Digitalmars-d-learn
mailing list