Re: Which is better - a char type or a string of length one?



On 31 Aug., 11:32, James Harris <james.harri...@xxxxxxxxxxxxxx> wrote:
On 31 Aug, 10:02, torb...@xxxxxxxxxxxxxx (Torben Ægidius Mogensen)
wrote:



James Harris <james.harri...@xxxxxxxxxxxxxx> writes:
On 27 Aug, 11:44, "bartc" <ba...@xxxxxxxxxx> wrote:

would the result be a character, or a string of length 1?

(For years I've been using a language with the latter approach, and it's
worked well (after all why should s[3] be that different from the slice
s[3..4]), with asc(s[3]) to get the character value.)

But which is better?

Were you querying the existence of a separate char data type or simply
the result of a substring reference?

Perhaps the answer depends on string representation. C supports a char
data type but its strings have a terminating zero so C has to
distinguish, doesn't it? A character occupies a single cell but a
string of one character needs at least two cells.

I'm in favour of having separate string and character types, primarily
because you by specifying a character type specify the expectation that
you get exactly one character in a way that can be cehcked statically.
If you just specify a string type, you would need runtime checks to
verify that there is exactly one character.

Could you not define the type of a character variable as "string of
length 1"?

With this argumentation every type should be defined
as array of length 1.

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
.



Relevant Pages

  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • Re: Soft-hyphens or breakable points in a string
    ... I specify a table width of 100%, but otherwise no cell dimensions are specified. ... An E-mail address is basically an unbreakable string that must not contain whitespace. ... URL or E-mail address needs to be broken, the break should appear "between elements, after a colon, a slash, a double slash, or the symbol @ but before a period or any other punctuation or symbols". ... If you enter a soft hyphen character, MS Word treats it as yet another graphic character and displayes it in all occasions. ...
    (comp.infosystems.www.authoring.html)
  • Re: Help Extracting Numbers formula
    ... way to tell that they're numeric numbers (data type: ... Microsoft Excel MVP ... find formula to pull out a number from a text string, ... starting from the leftmost character. ...
    (microsoft.public.excel.misc)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)