Currently, conditional and match statements exist, allowing different lists of statements to be executed based on the value of a guard expression.
However, there are obviously scripts with branching logic where code duplication can only be avoided by the use of conditional expressions, for example the value of an expression affecting the assignment of a variable:
# select a different primer based on the args.kit_a bool
primer = if args.kit_a {
'AGCTAGCTGATGC'
} else {
'AAGAGAGGAGGGTTTTCGATG'
}
if read matches [_ primer _ -primer _] => {
count!('match')
}
Note that these expressions would need to result in a value of the same type from each branch, so that the rest of the program can still be typed correctly.
Could also improve concision for many common scripts:
count!(if read.len() > 100 { 'long' } else { 'short' })
Here, I'm imagining a Rust-style design where each branch of a conditional expression can contain a number of statements, concluding with a single expression. Could alternatively use a design where each branch of a conditional expression only contains one expression, maybe signified by the forbidding of curly braces:
count!(if read.len() > 100 'long' else 'short')
Regardless, what do we do if interior statements or the final result expression produce effects? This becomes a problem because expressions should be pure:
category = if read.len() > 100 {
stdout!(read.id)
'polya'
} else {
'no polya'
})
Could just throw an error explicitly forbidding effects in these cases, if it wouldn't be too special-casey.
We would need completeness checking, to make sure that for all conditional expressions, one of the branches will resolve. This could be ensured by simply making all conditional expressions require an else branch to compile, as in Rust.
Note that, although they seem like a good idea, the possibility of match expressions would pose serious questions:
- What do we do if multiple matches are found?
- How do we check completeness? (would need to analyse eg whether a pattern is the complement of another pattern)
Hence, may be better to just consider conditional expressions for now.
Currently, conditional and match statements exist, allowing different lists of statements to be executed based on the value of a guard expression.
However, there are obviously scripts with branching logic where code duplication can only be avoided by the use of conditional expressions, for example the value of an expression affecting the assignment of a variable:
Note that these expressions would need to result in a value of the same type from each branch, so that the rest of the program can still be typed correctly.
Could also improve concision for many common scripts:
Here, I'm imagining a Rust-style design where each branch of a conditional expression can contain a number of statements, concluding with a single expression. Could alternatively use a design where each branch of a conditional expression only contains one expression, maybe signified by the forbidding of curly braces:
Regardless, what do we do if interior statements or the final result expression produce effects? This becomes a problem because expressions should be pure:
Could just throw an error explicitly forbidding effects in these cases, if it wouldn't be too special-casey.
We would need completeness checking, to make sure that for all conditional expressions, one of the branches will resolve. This could be ensured by simply making all conditional expressions require an else branch to compile, as in Rust.
Note that, although they seem like a good idea, the possibility of match expressions would pose serious questions:
Hence, may be better to just consider conditional expressions for now.