Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test a character for being a valid Julia one-character variable name?

Tags:

julia

How to test whether a single Unicode character is a valid variable name. The manual says:

Variable names must begin with a letter (A-Z or a-z), underscore, or a subset of Unicode code points greater than 00A0; in particular, Unicode character categories Lu/Ll/Lt/Lm/Lo/Nl (letters), Sc/So (currency and other symbols), and a few other letter-like characters (e.g. a subset of the Sm math symbols) are allowed.

Is there a function which tests a character to see if it's a valid variable name? isvalid() looks like it checks to see whether a character is a valid character, which might not be the same?

like image 237
daycaster Avatar asked Jan 28 '20 13:01

daycaster


People also ask

What characters are allowed as the first character for a valid variable name?

Variable names can be up to 64 bytes long, and the first character must be a letter or one of the characters @, #, or $. Subsequent characters can be any combination of letters, numbers, nonpunctuation characters, and a period (.).

How do you declare a variable in Julia?

Variables in Julia can be declared by just writing their name. There's no need to define a datatype with it. Initializing variables can be done at the time of declaring variables. This can be done by simply assigning a value to the named variable.

Which characters can be used in a variable name?

Variable names must be unique in a Dataset. Variable names are up to 64 characters long and can only contain letters, digits and nonpunctuation characters (except that a period (.) is allowed.

Which character should not be used as a single character variable names?

The variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in the variable names. Hence the correct answer is White space characters.

What are the rules for variable names in Julia?

Variable names in Julia must start with an underscore, a letter (A-Z or a-z) or a Unicode character greater than 00A0 (nbsp). Variable names can also contain digits (0-9) or !, but must not begin with these.

How do I assign a datatype to a variable in Julia?

Julia will assign the datatype automatically to the variable. Variable names in Julia must start with an underscore, a letter (A-Z or a-z) or a Unicode character greater than 00A0 (nbsp). Variable names can also contain digits (0-9) or !, but must not begin with these.

How does @Julia decide which characters to print as-is?

Julia uses your system's locale and language settings to determine which characters can be printed as-is and which must be output using the generic, escaped \u or \U input forms. In addition to these Unicode escape forms, all of C's traditional escaped input forms can also be used:

What is a char value in Julia?

In Julia, a Char (character) value represents a single character. Characters in Julia are 32-bit primitive data types. It has a special literal representation and appropriate arithmetic behaviours and these can be converted into a numeric value that represents a Unicode code point. Other subtypes of AbstractChar may be defined by Julia packages.


1 Answers

You can use Base.isidentifier for that:

julia> Base.isidentifier("a")
true

julia> Base.isidentifier("a′")
true

julia> Base.isidentifier("1a′")
false

julia> Base.isidentifier("â")
true
like image 95
pfitzseb Avatar answered Sep 18 '22 14:09

pfitzseb