I'm looking for some clarification on the compiler error message The value of xyz is undefined here, so reference is not allowed.
, together with the do-notation. I did not manage to generalize the example enough, all I can give is the concrete example where I stumbled upon this behaviour. Sorry for that.
Using purescript-parsing, I want to write a parser which accepts nested multiline-comments. To simplify the example, each comment starts with (
, ends with )
and can contain either an a
, or another comment. Some examples: (a)
and ((a))
accepted, ()
, (a
or foo
get rejected.
The following code results in the error The value of comment is undefined here, so reference is not allowed.
on the line content <- string "a" <|> comment
:
comment :: Parser String String
comment = do
open <- string "("
content <- commentContent
close <- string ")"
return $ open ++ content ++ close
commentContent :: Parser String String
commentContent = do
content <- string "a" <|> comment
return content
I can get rid of the error by inserting a line above content <- string "a" <|> comment
which as far as I understand it does not change the resulting parser at all:
commentContent :: Parser String String
commentContent = do
optional (fail "")
content <- string "a" <|> comment
return content
The questions are:
- What is happening here? Why does the extra line help?
- What is a non-hacky way to get the code to compile?