[Object] case: [Object] do: [Block]. | Manual

[Object] case: [Object] do: [Block].

This message makes the recipient compare itself to the specified object. If the recipient considers itself to be equal, it will carry out the instructions in the associated block of code. The recipient will send the message '=' to itself with the other object as an argument. This leaves it up to the recipient to determine whether the objects are considered equal. If the recipient decides the objects are not equal, the associated code block will be ignored. Note that this allows you to implement a so-called switch-statement like those found in other languages. Because of the generic implementation, you can use the case statements on almost any object. Case-do statements may provide a readable alternative to a long list of if-else messages. The example program below will print the text 'It's a Merlot!'.
Usage:


 #Can we use a switch statement in Citrine?

 #create a good wine
 ☞ wine := Object new.

 #define a string representation for this wine
 wine on: 'toString' do: {
	↲ 'merlot'.
 }.

 #define how wines are to be compared
 wine on: '=' do: { :other wine
	↲ ( me toString = other wine ).
}.

 #now select the correct wine from the list
 wine
	case: 'cabernet' do: { ✎ write: 'it\\'s a Cabernet!'. },
	case: 'syrah'    do: { ✎ write: 'it\\'s a Syrah!'.    },
	case: 'merlot'   do: { ✎ write: 'it\\'s a Merlot!'.   },
	case: 'malbec'   do: { ✎ write: 'it\\'s a Malbec!'.   }.

Welcome to the Citrine Online Manual Pages. This online manual has been generated automatically from source code comments using the shtml.ctr script. All Citrine documentation is also available as a UNIX manual file included in the download package. These pages are just a web version of the original Citrine Programming Language User Manual and Reference Guide.

Improve this manual

You can help to improve this manual. Any help is appreciated! If you spot an error in this manual or you want to contribute, just change the corresponding comment in the original Citrine Programming Language source code hosted on github and create a 'Pull Request'. After the documentation generation script has been invoked again the manual pages will be updated to reflect your contribution and so will these online manual pages.

About Citrine

Citrine is an easy to use programming language for everyone. Citrine is so simple it does not even need special syntax for loops or conditions. You can learn Citrine in just a couple of minutes. Citrine makes programming fun again! Visit the homepage for a quick tutorial.

back to homepage | back to manual menu | top of page