workflow convert_sam_to_bam {
    take:
    genrf_ch
    main:
    // def sam_to_bam_ch = Channel.empty()
    sam_to_bam_ch = Channel.empty()
    if (!params.sam.isBlank())
    {
        channel
            .fromPath(params.sam)
            .map{tuple(string_prefix(it.toString(), "convert_sam_to_bam"), it)}
            .combine(genrf_ch)
            .set{sam_ch}
        
        /*
        sam_to_bam(sam_ch)
            .set{sam_to_bam_ch}
        */
        sam_to_bam_ch = sam_to_bam(sam_ch)
    }
    
    emit:
    sam_to_bam_ch
}
In the code above, I get an error if I define sam_to_bam_ch with def.
When sam_to_bam_ch is defined with def, within the if statement, the statement
sam_to_bam(sam_ch)
        .set{sam_to_bam_ch}
Gives the error
ERROR ~ Missing name with which to set the channel variable
The statement
sam_to_bam_ch = sam_to_bam(sam_ch)
Gives the error
Missing workflow output parameter: sam_to_bam_ch
The code runs either way if I don’t use def to define sam_to_bam_ch.
I’d appreciate clarification on a few things:
- If I don’t use 
defwhen I define sam_to_bam_ch, will it have global scope? - I prefer to keep variable scope local when possible, is there a way to make sam_to_bam have local scope (while still being emittable) and get this code to work?
 - Why do the two statements calling 
sam_to_bamproduce different error messages? - Why does 
def sam_to_bam_chgenerate any error message at all in this case? My understanding of Groovy’s scoping is that its scope should extend into theifblock.