Re: Which is better - a char type or a string of length one?
- From: tm <thomas.mertes@xxxxxx>
- Date: Mon, 31 Aug 2009 02:57:54 -0700 (PDT)
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.
.
- References:
- Which is better - a char type or a string of length one?
- From: James Harris
- Re: Which is better - a char type or a string of length one?
- From: Torben Ægidius Mogensen
- Re: Which is better - a char type or a string of length one?
- From: James Harris
- Which is better - a char type or a string of length one?
- Prev by Date: Re: Which is better - a char type or a string of length one?
- Next by Date: Re: Which is better - a char type or a string of length one?
- Previous by thread: Re: Which is better - a char type or a string of length one?
- Next by thread: Computer soft & hard
- Index(es):
Relevant Pages
|