Allow can
and try
functions to return known results with unknown
values in the arguments
#622
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
can
andtry
functions can return more precise results in some cases. Rather than try to inspect the expressions for any unknown values, rely on the evaluation result to be correct or error and base the decision on the evaluated values and errors.A fundamental requirement for the
try
andcan
functions is that the value and types remain consistent as argument values are refined. This can be done provided we hold these conditions regarding unknowns to be true:As long as those conditions remain true, the result of the
try
argument's evaluation can be trusted, and we don't need to bail out early at any sign of an unknown in the argument expressions.Another probably stronger case for determining the result only based on the argument evaluation, is that the result of an expression stored in a variable should be handled consistently with the same argument given in-line. For example in Terraform these currently return 2 different results:
While the evaluation result of each argument can be trusted in isolation however, the fact that different types and values can be returned by
try
's multiple arguments means we need to convert the return to the most generic value possible to prevent inconsistent results ourself (adhering to the 3rd condition above). That means anything which is not entirely known must be converted to a dynamic value.Even more refinement might still be possible in the future if all arguments are evaluated and compared for compatibility, but care needs to be taken to prevent changing known values within collections from different arguments even when types are identical (as shown in the
nested index op on unknown
test).