Version 2 (modified by 11 years ago) ( diff ) | ,
---|
Matching
- Prefer named arguments
-
When matching a uniontype, prefer using named arguments to only match what is used. A match using named arguments only needs to be changed if the matched fields change in a uniontype, while a match using positional arguments will always have to be changed if the uniontype changes.
One exception is when matching all fields in a uniontype, doing some changes to them, and then constructing a new instance of the same type. In that case the code will need to be updated if the uniontype changes anyway, so using positional arguments will give cleaner code.
// Prefer this. case DAE.ARRAY(ty = array_ty) // Not this. case DAE.ARRAY(array_ty, _, _)
case DAE.ARRAY(array_ty, is_scalar, expl) equation expl = listRest(expl); then DAE.ARRAY(array_ty, is_scalar, expl)
- Use
else
for catch-all -
Prefer using
else
when writing a catch-all case:outValue := match(inExp1, inExp2) local Integer i1, i2; case (DAE.ICONST(integer = i1), DAE.ICONST(integer = i2)) then i1 + i2; else 0; // Avoid this: case (_, _) then 0; // And this (using 'then' is legal, but unnecessary): else then 0; end match;
- Use
as
only for partial matches -
as
is usually not necessary when matching a whole input argument, such as this:Prefer using the input parameters directly instead:_ := match(inExp, inStr) local DAE.Exp exp; String str; case (exp as DAE.CALL(path = Absyn.IDENT("sum")), _) then ExpressionDump.printExpStr(exp); case (_, str as "someString") then str; end match;
outStr := match(inExp, inStr) case (DAE.CALL(path = Absyn.IDENT("sum")), _) then ExpressionDump.printExpStr(inExp); case (_, "someString") then inStr; end match;
Note:
See TracWiki
for help on using the wiki.