Assigning to a variable before use in a conditional #3869
-
I would like to be able to assign a variable before using it in the conditional. To do that I believe that I need to change Drasil/code/drasil-code/lib/Language/Drasil/Code/Imperative/Modules.hs Lines 326 to 334 in 6033d40 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@balacij or @B-rando1 should be able to help - as well as make sure your suggested change is the 'right' one to make. [I think what you're trying to do at a high level is right, but your suggested change doesn't seem quite right to me, as explained.] |
Beta Was this translation helpful? Give feedback.
-
I've looked a few times at this. If we want to create the variable directly above the I'm not exactly sure how it'll work with the types, but roughly what I think we need to do is modify the function like this: chooseConstr :: (OOProg r) => ConstraintBehaviour ->
[(CodeVarChunk, [ConstraintCE])] -> GenState [MSStatement r]
chooseConstr cb cs = do
varDefs <- mapM constrVarDef cs -- constrVarDef needs to act like `constrWarn` to generate either an `emptyStmt` or a `varDecDef`, depending on constraint type
conds <- mapM (\(q,cns) -> mapM (convExpr . renderC q) cns) cs
bods <- mapM (chooseCB cb) cs
return $ concat $ zipWith $ (someting_here_probably (concat $ zipWith (zipWith (\cond bod -> ifNoElse [((?!) cond, bod)]))))
varDefs conds bods
where chooseCB Warning = constrWarn
chooseCB Exception = constrExc That's pretty rough and definitely wouldn't compile as-is 😅, but I think that's roughly what you need to do. Let me know if you need more to go on, I can look a bit more at the specifics if you need help. |
Beta Was this translation helpful? Give feedback.
I've looked a few times at this. If we want to create the variable directly above the
if
-statement, this function is the place that makes the most sense to me. Because it returns a list of statements, it would be difficult to insert thevarDecDef
s after this point.I'm not exactly sure how it'll work with the types, but roughly what I think we need to do is modify the function like this: