Forum > Suggestions

CASE statement with strings.

(1/7) > >>

Mike James:
I find this construct useful in other languages - how about a CASE statement that can select on strings i.e.

case Foo of
    'bar01': {do something}
    'bar02': {do something else}
end;

=mike=-

Knipfty:
Hi Mike,

This is from the Pascal online manual http://www.freepascal.org/docs-html/ref/refsu47.html


--- Quote ---The constants appearing in the various case parts must be known at compile-time, and can be of the following types : enumeration types, Ordinal types (except boolean), and chars. The case expression must be also of this type, or a compiler error will occur. All case constants must have the same type.
--- End quote ---

So you can use chars, but not strings in a CASE statement.

I agree it could be useful, but other languages, such as BASIC, are less effcient, and often use variants.

Knipfty

Mike James:

--- Quote from: Knipfty on August 27, 2012, 04:10:33 pm ---Hi Mike,

This is from the Pascal online manual http://www.freepascal.org/docs-html/ref/refsu47.html


--- Quote ---The constants appearing in the various case parts must be known at compile-time, and can be of the following types : enumeration types, Ordinal types (except boolean), and chars. The case expression must be also of this type, or a compiler error will occur. All case constants must have the same type.
--- End quote ---

So you can use chars, but not strings in a CASE statement.

I agree it could be useful, but other languages, such as BASIC, are less effcient, and often use variants.

Knipfty

--- End quote ---

True - but I'm looking at it as an extension to the language. As the string used in the selection would be an immutable type then it would not break the rule that it should be known at compile time.

-=mike=-

Martin_fr:
This was implemented already and works in fpc 2.6.0


--- Code: ---{$mode objfpc}
var s: string;
begin
  case s of
    'abc': s := '123';
    '111': s := '';
  end;
end.

--- End code ---

Mike James:

--- Quote from: Martin_fr on August 27, 2012, 04:29:02 pm ---This was implemented already and works in fpc 2.6.0


--- Code: ---{$mode objfpc}
var s: string;
begin
  case s of
    'abc': s := '123';
    '111': s := '';
  end;
end.

--- End code ---

--- End quote ---

Excellent - time to upgrade methinks  :D

btw the FPC 2.6.0 reference guide (pdf) makes no mention of this...

-=mike=-

Navigation

[0] Message Index

[#] Next page

Go to full version