I understand what type constraints are and how we can use them, e.g. from this guide written by @Stephen.
Now I’m trying to apply a type constraint to a function signature which is a record field type, and get the following error:

Am I doing something wrong, or this is not allowed in Daml?
There are two likely possibilities:
- You want
given
, when
, and then_
to be polymorphic over m
even given a single Test'
value. Your declaration means that each Test'
serves exactly one m
; if that is not what you mean, remove m
from Test'
's type parameters, and type given : forall m. (CanAbort m) => m a
and likewise for the other functions. (Because this is a more exotic form of type polymorphism, Daml can’t figure out where the m
should be inferred otherwise.)
- You want
m
to be fixed to a particular m
for each Test'
. In that case, there is no benefit to putting the constraints in the record structure; typeclass coherence means that if you satisfy the constraint when creating the Test'
, it will give exactly the same solution to the constraint as if you satisfied it when extracting and using one of the functions. So you might as well move the CanAbort m
constraint to whatever function produces Test'
values.
1 Like
One other note: for this forum, it is preferred that you paste code and error messages as text in blocks as explained here, rather than screenshots. That way, for example, I could simply copy-and-paste your sample code and edit it; not a big deal here but definitely important for bigger samples.
1 Like
Thank you, @Stephen for the answer, #2 works perfectly, and also the reminder to avoid screen shots, I will follow this guidance. (It’s tempting to insert a screen shot because it displays the error message, while the copy+pasted code doesn’t. )
I think this used to not work in VS Code, but as of Code 1.67.2 at least, you can move the mouse into the error tooltip, select text, and copy with Ctrl-C/Cmd-C. I highly recommend this for sharing errors from Code.
2 Likes