We've moved!
For WDL questions, see the WDL specification and WDL docs.
For Cromwell questions, see the Cromwell docs and please post any issues on Github.

Error when using `prefix()` with an optional array.

Is it possible to use prefix() with an optional input?

I was trying to do something like this:

task DoSomething {
    Array[File] ? intervals = []

    command {
        ${sep=" " prefix("--intervals ", intervals)}

However I get this error:
java.lang.UnsupportedOperationException: Could not evaluate expression: prefix("--intervals", intervals)

But it works as expected when I use it this way:

task DoSomething {
    Array[File] intervals

    command {
        ${sep=" " prefix("--intervals ", intervals)}

Is this expected? Or am I doing something wrong?


  • mcovarrmcovarr Cambridge, MAMember, Broadie, Dev ✭✭

    That behavior is consistent with the specification. You should be able to use select_first to choose between the optional array and an empty array to do what I think you're trying to do.

  • ChrisLChrisL Cambridge, MAMember, Broadie, Dev admin
    edited December 2017

    @TheCanesBoy2010 more detail:

    An optional array is different from an empty array.
    You can have an empty non-optional array (Array[File] intervals = [])
    You can have an optional array which is given a value with entries (Array[File]? intervals = ["/path/to/a", "/path/to/b"])

    If it helps, you might think of it as the difference between:

    • On the one hand being forced to supply an array, but the array you supplied is allowed to have 0 elements (Array[File])
    • On the other hand, being allowed to choose whether or not to supply an array at all - and of course the array you choose to supply can still be empty ((Array[File]?)

    You might also be interested in the non-empty array modifier, which guarantees that the array has at least one element: Array[File]+ intervals = ["/path/to/a"].

    One last twist, you can also have an optional non-empty array: Array[File]+?, in this case you're free to supply a value, or not, but if you do choose to supply a value, it must have at least one entry in it.

  • orodehorodeh Mountain View, CAMember

    The last bit was really surprising to me. I did not realize that a type like Array[File]+? exists. I have never seen code that uses it.

    Perhaps the existence of this type should be clearly mentioned in the SPEC, or, explicitly disallowed.

  • ChrisLChrisL Cambridge, MAMember, Broadie, Dev admin

    @orodeh yeah, it's a natural extension of the facts that

    • Array[File]+ is a non-empty array
    • Any type can be made optional

    But I agree, it might be a good idea to call it out explicitly since it does look kinda funny

  • orodehorodeh Mountain View, CAMember

    For context, in the DNAnexus type system, we have optional and non-empty, but not both. The dxWDL compiler converts between the WDL type system, and the native type system. Seeing this post, I realized that there is a WDL type that lacks a natural DNAx representation.

  • edited February 2018

    Awesome. Thanks @ChrisL this helps a lot

Sign In or Register to comment.