I am coming from an intermediate Haskell background (and before that Python, Perl, C et al.)
I am trying to learn Purescript to do front end development (for the first time). I’ve realized that I’m essentialy learning two things at once here.
I have the following code, trying to do some simple DOM manipulation using Web.HTML and Web.DOM. Everything seems to work until I try to use the appendChild function (or possibly it’s due to my improper unwrapping of a datum that is a Maybe Element??).
Here’s my code so far:
module Main where
import Prelude
import Data.Maybe (Maybe(..), fromMaybe)
import Effect (Effect)
import Effect.Console (log)
import Web.HTML (window)
import Web.HTML.Window (document)
import Web.HTML.HTMLDocument (toNonElementParentNode, toDocument)
--import Web.HTML.HTMLDocument (toParentNode, HTMLDocument)
import Web.DOM.Element (Element, toNode, setId)
import Web.DOM.NonElementParentNode (getElementById)
import Web.DOM.Node (textContent, setTextContent, appendChild)
import Web.DOM.Document (createElement)
import Debug.Trace (traceM)
maybeText :: Maybe Element -> Effect String
maybeText (Just el) = textContent (toNode el)
maybeText _ = pure ""
main :: Effect Unit
main = do
-- Web.HTML (window)
-- window :: Effect Window
w <- window
-- Web.HTML.Window (document)
-- document :: Window -> Effect HTMLDocument
d <- document w
-- Web.HTML.HTMLDocument (toNonElementParentNode)
-- toNonElementParentNode :: HTMLDocument -> NonElementParentNode
let pofd = toNonElementParentNode d
-- getElementById :: String -> NonElementParentNode -> Effect (Maybe Element)
elem_content <- getElementById "content" pofd
-- maybeText :: Maybe Element -> Effect String
elem_content_str <- maybeText elem_content
-- log :: String -> Effect Unit
log $ "content id contains: " <> elem_content_str
-- toDocument :: HTMLDocument -> Document
let d_plain = toDocument d
-- const item3Element = document.createElement("li"); // Create an "li" element
-- createElement :: String -> Document -> Effect Element
elem_item3 <- createElement "li" d_plain
-- item3Element.id = "item3"; // Define element ID
-- setId :: String -> Element -> Effect Unit
setId "item3" elem_item3
-- item3Element.textContent = "Item 3"; // Define its text content
-- toNode :: Element -> Node
-- setTextContent :: String -> Node -> Effect Unit
setTextContent "Item 3" $ toNode elem_item3
-- fromMaybe :: forall a. a -> Maybe a -> a
-- fromMaybe :: Element -> Maybe Element -> a
let elem_content_unwrapped = fromMaybe (elem_item3) (elem_content)
-- document.getElementById("content")
-- .appendChild(item3Element); // Insert the new element into the DOM
-- toNode :: Element -> Node
-- appendChild :: Node -> Node -> Effect Node
appendChild (toNode elem_item3) (toNode elem_content_unwrapped)
log "end" -- dummy last command
The specific error I’m getting is:
Error found:
in module Main
at src/Main.purs:83:3 - 85:36 (line 83, column 3 - line 85, column 36)
A result of type
Node
was implicitly discarded in a do notation block.
You can use _ <- ... to explicitly discard the result.
while applying a function discard
of type Discard t0 => Bind t1 => t1 t0 -> (t0 -> t1 t2) -> t1 t2
to argument (appendChild (toNode elem_new)) (toNode elem_content_unwrapped)
while inferring the type of discard ((appendChild (toNode elem_new)) (toNode elem_content_unwrapped))
in value declaration main
where t0 is an unknown type
t2 is an unknown type
t1 is an unknown type
See https://github.com/purescript/documentation/blob/master/errors/NoInstanceFound.md for more information,
or to contribute content related to this error.
[error] Failed to build.
I have tried several things here, such that I’m not sure that I’m thinking straight any more.