$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
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:
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)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?
write $case(condition, 1:"hello " _ "world", 2:"name", :"!")
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:
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.
Choose to your taste:
Class dc.test [ Abstract ]
{
ClassMethod Public() [ Internal, Private, ProcedureBlock = 0 ]
{
q
Choice0()
q "Sunday"
Choice1()
q "Monday"
Choice2()
q "Tuesday"
Choice3()
q "Wednesday"
Choice4()
q "Thursday"
Choice5()
q "Friday"
Choice6()
q "Saturday"
Choice()
set a="entry " _"error"
return a
}
/// d ##class(dc.test).Test()
ClassMethod Test()
{
s daynum=$zd($h,10)
w "1) ",$case(daynum,
0:$$Choice0,
1:$$Choice1,
2:$$Choice2,
3:$$Choice3,
4:$$Choice4,
5:$$Choice5,
6:$$Choice6,
:$$Choice),!
w "2) ",@("$$Choice"_$case(daynum,
0:0,
1:1,
2:2,
3:3,
4:4,
5:5,
6:6,
:"")),!
s daynum="-"
w "3) ",$case(daynum,
0:"Sunday",
1:"Monday",
2:"Tuesday",
3:"Wednesday",
4:"Thursday",
5:"Friday",
6:"Saturday",
:$xecute("()"_
" set a=""entry "" _""error"""_
" return a")),!
}
}