Continuing down the rabbit hole of type errors, I have a function to
tell me if a character in a string needs to be escaped, and how to do
it:
fun escape {c:int8} (c : char c) : [rc:int8 | rc != 0] @(bool, char rc) =
case c of
| '\n' => @(true, 'n')
(* more cases snipped *)
| '\0' => @(true, '0')
| _ => @(false, c)
Now, patsopt
chokes on this with
error(3): unsolved constraint: C3NSTRprop(main; S2Eapp(S2Ecst(!=); S2EVar(4098->S2Evar(c(7574))), S2Eintinf(0)))
which I’ve decoded to
error(3): unsolved constraint: c != 0
Which looks like ATS sees the path through _
returning c, and is
trying to show that c
satisfies the constraints that rc
has, which
doesn’t work. Yet, I believe I’ve handed the case of c == '\0'
, so I
would expect it to see that if we put 0
in, then rc == char '0'
, and
thus the constraint is satisfied. I’m assuming that I’ve hit a case
where what I’ve done is safe, but the type checker can’t understand why.
(To preemptively answer a likely question, [rc:int8 | rc != 0]
is
needed because strnptr_set_at_gint
takes a charNZ
which is typed
similarly.)
So, is there either (a) a way to overrule the type checker and say “No! I
assure that this code is typed correctly!”, or (b) a way to structure
this function such that the type checker will understand its correctness?
(Surprisingly, doing _ => ( assertloc (c != 0) ; @(false, c) )
doesn’t
fix the issue?)
And as I’m still working on the previous question I asked, I’ll add
here that $showtype
is exceedingly useful for trying to understand
typing issues. I don’t see it publicly documented anywhere, so might I
suggest doing so?