$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?
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:
{
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>
Post Teste method?
Thanks Gilberto, this is a possible way, thanks.
Hi, Ed!
Thanks!
I'll elaborate the problem a bit. Here is the $case everybody loves:
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:
Thanks Vitaly!
Really great!
Classmethod Public: is it to be able to call for $$functions in a class?
Sorry for copying and pasting
thank you
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" ;)
Pinging @Alexander Koblov
Isn't it more like anonymous functions?
Currently not possible. Check @Gilberto Junior solution - it seems the closest to what you want.
Like this?
Hello world is a multiline expression.
Yes, this (Classmethod Public) is only necessary for the second example.
how about...
write $zdate($h,12)
I'd go with if, but you can also resolve case expression beforehand:
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.
Choose to your taste: