Skip to content
Discussions/App Development/Couldn't match type errorForum ↗

Couldn't match type error

App Development6 posts481 viewsLast activity Sep 2022
RO
ronihOP
Sep 2022

Hi,All

I have the following structures:

data Life_Type = Life_Type with
id : Optional Text
coverage : [Coverage_Type]

data Coverage_Type = Coverage_Type with
productCode : Optional Text

When I try to do:
let coverageLayerList = life.coverage
filteredByProductCode = filter (\r → r.productCode == Some “product”) coverageLayerList

The following error message appeared:

Can you tell me what I’m doing wrong?

CO
cocreature
Sep 2022

Could you share a bit more of your code? I can’t spot anything in the snippet you posted but I suspect the way you use filteredByProductCode may be the problem.

It would be great if you could embed your code in a code block to make sure indentation is preserved and it’s easier to read. Just wrap it in triple backticks:

```
yourcode is here
```
RO
ronih
Sep 2022

@cocreature
Thank you . Here is the relevant code snippet

CO
cocreature
Sep 2022

The issue is in the type of coverageType. First, remember that all branches in a case statement must have the same type.
Looking at the first branch, you return None. So coverageType must be of type Optional a for some type a. That’s the Optional a0 you see in the error.
Looking at the second branch, you return xs which is one of the elements of filteredByProductCode. filteredByProductCode must have the same type as coverageLayerList since filter doesn’t change the type of the list. But now we have a problem: life.coverage has type [Coverage_Type] so a single element has type Coverage_Type. But in the first branch, we said a single element must have type Optional a which doesn’t match. That’s exactly what the error is telling us.
Changing in (xs, …) to in (Some xs, …) should at least fix that type error, not sure if that’s what you intended.

The fact that errors sometimes don’t point at the location that you expect is one of the downsides of the relatively comprehensive type inference that Daml gives you. If an error seems confusing, I recommend to add more type annotations to narrow it down. E.g., in this case adding a type annotation to filteredByProductCode should narrow it down.

RO
ronih
Sep 2022

@cocreature
Thank you very much!
This really helped me understand the problem and learn from it

ST
Stephen
Sep 2022

Please use ``` around both code samples and errors, and avoid posting screenshots of code or errors, as explained here.

← Back to Discussions