Clarifying typechecking of optional values
See original GitHub issueSpec gray area:
If I have an optional declaration T? x
, and an expression refers to x
in some context where (non-optional) T
is expected, when should this be statically rejected as a type mismatch error, vs. considered valid albeit liable to fail at runtime?
The spec right now says x
“can only be used in calls or functions that accept optional values” but this isn’t quite clear to the above.
My “first principles” instinct would be that T? x
should statically fail to unify with (non-optional) T
absent some explicit coercion, but:
- It’s expressly valid and desirable for interpolations in task commands, as described in “Prepending a String to an Optional Parameter.” Also relevant #227
- If one writes another declaration
T y = x
what should happen when? - I infer from some examples in the wild that Cromwell accepts the pattern like
T y = if defined(x) then EXPR_WITH_x else SOME_DEFAULT
(ex1, ex2).- In those examples,
EXPR_WITH_x
isx * 1000
andsize(x)
respectively. - Do we check the optional quantifier statically at all?
- Alternatively, one could imagine special-case typechecking logic for the
if defined(x) ...
case.
- In those examples,
- If one specifies
x
to a required call input of typeT
what should happen when? (ex)
Appreciate if someone from Cromwell team could clarify how it approaches this (and of course comment on what would be ideal for the spec). Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
typescript : type check optional parameter in a Function ...
problem I have is with optional parameter, for case e, I could add another conditional type check. Typescript : v 4.1. typescript ·...
Read more >Static Typing in Python - Towards Data Science
Type annotations in Python are helpful for debugging and optional type checking that mimics static typing. It becomes increasingly popular in ...
Read more >Type-clarifying comments and type checking in dynamic languages ...
Here's the thing. Statically-typed languages normally have a compiler, and the compiler does type checking to prevent a certain class of bugs at...
Read more >RFC: More general type checking for structural typings
Setting the fields to None / undefined crashes it. I've recently bound to it in ReScript with @obj for optional fields.
Read more >Type system - Wikipedia
For variables, the type system determines the allowed values of that term. Type systems formalize and enforce the otherwise implicit categories the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This does indeed come up in discussions a lot!
I’ve often found that what seemed like “special cases to make things easier” has instead lead to confusion and a harder to understand language. And there are a lot of special cases around optionals (as you mentioned) that I regularly find more confusing than helpful.
So my gut feeling is to be stricter on enforcing “you can’t use optionals without unpacking them first” as long as we allow people to unpack things easily if they need to. Unfortunately the “current” way of unpacking a
Int? x
intoInt
is rather ugly:select_first([x])
.I’d be open to either a structural or expression based way of unpacking. I think @patmagee 's suggestion against my structural https://github.com/openwdl/wdl/issues/187 was very good and far less intrusive - he suggested that we could have a simple
select(x)
orget(x)
function to turn declarations likeInt? x
intoInt
or else throw an error. I think this would free us up to be a lot more aggressive with type-checking of optionals.Re “task commands”, I think a recent PR (still awaiting implementation) was involved in tidying up those command sections to only allow simple expressions rather than their own entire complex syntax. I wonder if that addressed optionals.
Re the final example about optional inputs - I think that this is now tidied up in WDL 1.0. In WDL 1.0 the example would be able to indicate “the input has a default so might not be supplied, but the type of the value is not optional” like this:
I have written a straw-man spec diff for debate & discussion: https://github.com/openwdl/wdl/pull/290