Written by

Senior Startups and Community Programs Manager at InterSystems Corporation
Question Evgeny Shvarov · Jun 5, 2018

$CASE For Long Stories

Hi, Community!

$CASE is a fine syntax sugar for "Ifs" with one-line/one-word expressions, like in docs:

SET daynum=$ZDATE($HOROLOG,10)
  WRITE $CASE(daynum,
              1:"Monday",2:"Tuesday",3:"Wednesday",
              4:"Thursday",5:"Friday",
              6:"Saturday",0:"Sunday",:"entry error")

But if my expression for a current case is a multi-line business logic? Can I use "{}" somehow or better go with "IF" instead?

Comments

Gilberto Junior · Jun 5, 2018

Hi in this case I have already used a function that returns a string, with this I came to a utility very similar to the switch case of other languages Sorry for the possible inconsistency in the response, it was translated by google translator

Example:

Class Case.Teste Extends %SerialObject
{
ClassMethod Teste(pReq As %Integer)
{
WRITE $CASE(pReq,
              1:..Oi(),2:..Tchau(),: ..GOKU())
}
ClassMethod Oi() As %String
{
Quit "OI sou do Brasil"
}
ClassMethod Tchau() As %String
{
Quit "Um abraço"
}
ClassMethod GOKU() As %String
{
Quit "Oi Eu sou o GOKU"
}
Storage Default
{
<StreamLocation>^Case.TesteS</StreamLocation>
<Type>%Library.CacheSerialState</Type>
}
}

USER>do ##Class(Case.Teste).Teste(1)
OI sou do Brasil


USER>do ##Class(Case.Teste).Teste(2)
Um abraço


USER>do ##Class(Case.Teste).Teste(3)
Oi Eu sou o GOKU


USER>

0
Evgeny Shvarov  Jun 5, 2018 to Gilberto Junior

Thanks Gilberto, this is a possible way, thanks. 

0
Evgeny Shvarov  Jun 5, 2018 to Eduard Lebedyuk

Hi, Ed!

Thanks! 

I'll elaborate the problem a bit. Here is the $case everybody loves:

write $case(condition, 1:expression1, 2:expression2, expression3)

My problem is that with business logic change the expression1 and expression2 showed the need for multiline logic. And I was looking for something like:

write $case(condition,

1: {

New cool logic for expression 1 with line 1
and line 2


},

2:{

Perfect, better logic for expression2
maybe with 
few lines
more

},

expression3)
0
Evgeny Shvarov  Jun 6, 2018 to Vitaliy Serdtsev

Thanks Vitaly!

Really great!

Classmethod Public: is it to be able to call for $$functions in a class?

0
Gilberto Junior  Jun 5, 2018 to Eduard Lebedyuk

Sorry for copying and pasting
thank you

0
Evgeny Shvarov  Jun 5, 2018 to Eduard Lebedyuk

Like this:

write $case(condition,             1:{ 
                 set a="hello " _"world"
                 return a
               }    
             2:"name",:"!")

I figured out that the thing I want is close to "closures" ;)

0
Eduard Lebedyuk  Jun 5, 2018 to Evgeny Shvarov

Like this?

write $case(condition,             1:"hello " _"world",             2:"name",:"!")

Hello world is a multiline expression.

0
Vitaliy Serdtsev  Jun 6, 2018 to Evgeny Shvarov

Yes, this (Classmethod Public) is only necessary for the second example.

0
Eduard Lebedyuk · Jun 5, 2018

I'd go with if, but you can also resolve case expression beforehand:

set condition = .... several lines of logic...
write $case(condition, ...)

And condition then can be separaten into several different lines.

I'm against method chaining in general and several functions on one line/step in particular.

Each line should contain one small operation.

0
Vitaliy Serdtsev · Jun 6, 2018

Choose to your taste:

<FONT COLOR="#000080">Class dc.test </FONT><FONT COLOR="#000000">[ </FONT><FONT COLOR="#000080">Abstract </FONT><FONT COLOR="#000000">]
{

</FONT><FONT COLOR="#000080">ClassMethod </FONT><FONT COLOR="#000000">Public() [ </FONT><FONT COLOR="#000080">Internal</FONT><FONT COLOR="#000000">, </FONT><FONT COLOR="#000080">Private</FONT><FONT COLOR="#000000">, </FONT><FONT COLOR="#000080">ProcedureBlock </FONT><FONT COLOR="#000000">= 0 ] {   </FONT><FONT COLOR="#0000ff">q </FONT><FONT COLOR="#ff0000">Choice0</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">q </FONT><FONT COLOR="#008000">"Sunday" </FONT><FONT COLOR="#ff0000">Choice1</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">q </FONT><FONT COLOR="#008000">"Monday" </FONT><FONT COLOR="#ff0000">Choice2</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">q </FONT><FONT COLOR="#008000">"Tuesday" </FONT><FONT COLOR="#ff0000">Choice3</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">q </FONT><FONT COLOR="#008000">"Wednesday" </FONT><FONT COLOR="#ff0000">Choice4</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">q </FONT><FONT COLOR="#008000">"Thursday" </FONT><FONT COLOR="#ff0000">Choice5</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">q </FONT><FONT COLOR="#008000">"Friday" </FONT><FONT COLOR="#ff0000">Choice6</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">q </FONT><FONT COLOR="#008000">"Saturday" </FONT><FONT COLOR="#ff0000">Choice</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">a</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">"entry " </FONT><FONT COLOR="#000000">_</FONT><FONT COLOR="#008000">"error"   </FONT><FONT COLOR="#0000ff">return </FONT><FONT COLOR="#800000">a </FONT><FONT COLOR="#000000">}

</FONT><FONT COLOR="#000080">/// d ##class(dc.test).Test() ClassMethod </FONT><FONT COLOR="#000000">Test() {   </FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">daynum</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$zd</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$h</FONT><FONT COLOR="#000000">,10)

  </FONT><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#008000">"1) "</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#0000ff">$case</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">daynum</FONT><FONT COLOR="#000000">,                 0:</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">Choice0</FONT><FONT COLOR="#000000">,                 1:</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">Choice1</FONT><FONT COLOR="#000000">,                 2:</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">Choice2</FONT><FONT COLOR="#000000">,                 3:</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">Choice3</FONT><FONT COLOR="#000000">,                 4:</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">Choice4</FONT><FONT COLOR="#000000">,                 5:</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">Choice5</FONT><FONT COLOR="#000000">,                 6:</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">Choice6</FONT><FONT COLOR="#000000">,                 :</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">Choice</FONT><FONT COLOR="#000000">),!

  </FONT><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#008000">"2) "</FONT><FONT COLOR="#000000">,@(</FONT><FONT COLOR="#008000">"$$Choice"</FONT><FONT COLOR="#000000">_</FONT><FONT COLOR="#0000ff">$case</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">daynum</FONT><FONT COLOR="#000000">,                               0:0,                               1:1,                               2:2,                               3:3,                               4:4,                               5:5,                               6:6,                               :</FONT><FONT COLOR="#008000">""</FONT><FONT COLOR="#000000">)),!

  </FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">daynum</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">"-"      </FONT><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#008000">"3) "</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#0000ff">$case</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">daynum</FONT><FONT COLOR="#000000">,                 0:</FONT><FONT COLOR="#008000">"Sunday"</FONT><FONT COLOR="#000000">,                 1:</FONT><FONT COLOR="#008000">"Monday"</FONT><FONT COLOR="#000000">,                 2:</FONT><FONT COLOR="#008000">"Tuesday"</FONT><FONT COLOR="#000000">,                 3:</FONT><FONT COLOR="#008000">"Wednesday"</FONT><FONT COLOR="#000000">,                 4:</FONT><FONT COLOR="#008000">"Thursday"</FONT><FONT COLOR="#000000">,                 5:</FONT><FONT COLOR="#008000">"Friday"</FONT><FONT COLOR="#000000">,                 6:</FONT><FONT COLOR="#008000">"Saturday"</FONT><FONT COLOR="#000000">,                 :</FONT><FONT COLOR="#0000ff">$xecute</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"()"</FONT><FONT COLOR="#000000">_                           </FONT><FONT COLOR="#008000">" set a=""entry "" ""error"""</FONT><FONT COLOR="#000000">                           </FONT><FONT COLOR="#008000">" return a"</FONT><FONT COLOR="#000000">)),! }

}</FONT>

0