A generic case for strings

Do you remember the discussion about a case statement for strings?

I got this flash idea after reading Jolyon Smith’s “The case for case[]”, and remembering a comment from Francisco Ruiz on Nick Hodges’ article on THTMLWriter which suggested using a default array property in a creative fashion.

Honestly, it is not really a true case statement, and it might not be as fast as an if then else, but here is how it looks when used. A bit ugly. but good fun 🙂

program TestGenericsSwitch;
{$apptype Console}
['Any', procedure begin
Writeln('Definitively any case');
['B', procedure begin
Writeln('B all you can B');
['Charlie', procedure begin
Writeln('Checkpoint C');
.ElseCase(procedure begin
Writeln('Else what?');

And here is how it is implemented.

unit GenericsSwitch;

/// Written by Lars Fosdal , December 1, 2010

SysUtils, Generics.Collections;

TSwitchProc = reference to procedure;
TGenericSwitch = class(TObjectDictionary)
FTheElseCase: TSwitchProc;
FTheTargetKey: KeyType;
function AddSwitchCase(const name: KeyType;
const value: TSwitchProc): TGenericSwitch;
procedure SetTheElseCase(const Value: TSwitchProc);
procedure SetTheTargetKey(const Value: KeyType);
function ValidateKey(Key:KeyType):KeyType; virtual;
property TheTargetKey:KeyType read FTheTargetKey write SetTheTargetKey;
property TheElseCase:TSwitchProc read FTheElseCase write SetTheElseCase;
class function CaseOf(const Key: KeyType):TGenericSwitch;
function ElseCase(const Action: TSwitchProc): TGenericSwitch;
procedure EndCase;
property Cases[const name:KeyType; const value:TSwitchProc]: TGenericSwitch
read AddSwitchCase; default;

TStringSwitch = class(TGenericSwitch)
function ValidateKey(key:String):String; override;


{ TGenericSwitch }

function TGenericSwitch.AddSwitchCase(const name: KeyType; const value: TSwitchProc): TGenericSwitch;
Result := Self;
Add(ValidateKey(Name), Value);

class function TGenericSwitch.CaseOf(const Key: KeyType): TGenericSwitch;
Result := Create;
Result.TheTargetKey := Key;

function TGenericSwitch.ElseCase(const Action: TSwitchProc): TGenericSwitch;
Result := Self;
TheElseCase := Action;

procedure TGenericSwitch.EndCase;
DoIt : TSwitchProc;
if TryGetValue(TheTargetKey, DoIt)
then DoIt
if Assigned(TheElseCase)
then TheElseCase;

procedure TGenericSwitch.SetTheElseCase(const Value: TSwitchProc);
FTheElseCase := Value;

procedure TGenericSwitch.SetTheTargetKey(const Value: KeyType);
FTheTargetKey := ValidateKey(Value);

function TGenericSwitch.ValidateKey(Key: KeyType):KeyType;
Result := Key;

{ TStringSwitch }

function TStringSwitch.ValidateKey(key: String): String;
Result := LowerCase(Key);


10 thoughts on “A generic case for strings

  1. fwiw – I think the fact that you *can* do this (and have done) is interesting and clever.But seriously, we should not have to use such ugly and cumbersome constructs – these things should be added directly to the language.The fact that we can work around these gaps in the language is no excuse. We shouldn't have to.If anything, I think the resulting mess that arises from using these new language features demonstrates better than any reasoned argument why using them to plug such gaps is such a bad idea.It just smells bad. Very bad.But, as I say, sincerely – very neat, and really very clever. I just won't let it anywhere near my code. 🙂

  2. Very neat! I confess I had to read it a few times before I followed the implementation though. I kept on thinking the core 'trick' was something to do with the use of generics or anonymous methods, when it was really to do with a good ol' default property…

  3. @Jolyon – It isn't as dirty as it first looks. It will crash on duplicates, but that is “desirable” and in theory can be safeguarded against. The syntax is reasonably solid, and with a little tinkering, it could be used as a “reusable” case statement, ie create once, and call multiple times, instead of like now, where it destroys itself after each use. @Gad – I agree it should be a language feature to have string case statements.@Chris – I first did a string only variation, without the “ElseCase”, which simply tested as the statements were added, if they matched the key, and stopped processing AddSwitchCases after a match was found. Then I thought that if I make it generic, I could allow using any type as the case id (case MethodPointer of, etc.). It would look even cleaner if the anonymous code syntax would allow code fragments (one-liners, or begin/end blocks). Then there is the issue with not being able to assign result values to the outer methdod, directly from within the anon.method.

  4. Lars, it doesn't much matter how dirty it actually it is… it just looks dirty. If it's actually “clean” under the hood, if anything that makes its apparent dirtiness less excusable, not more. imho.FYI – it inspired me to create a “Pure Pascal” alternative (actually, I should say it reminded me of an old technique that I have regurgitated for hopefully a new audience). 🙂

  5. @Jolyon – I would probably have found your post through Delphifeeds. @Sergey – My bad! I hadn't seen your implementation. Although they both share similar features, like the default property use, they still are quite different. But what can I say? Great minds think alike? 😉

  6. Another point do this, instead of using StringToIndex and case, is that this method is resiliant regards to adding, removing or reordering cases. With StringToIndex, you have to “manually” maintain coherence between the string and the case index. How many have been burned by that little detail? I know I have.

Leave a Reply