3

I'm absolutely new to purescript and halogen. I'm trying to display an halogen component (myButton) when an html element exists, and do nothing otherwise.

displayButton :: Eff (HA.HalogenEffects ()) Unit
displayButton = HA.runHalogenAff do
  containerElement <- HA.selectElement (QuerySelector "#halogen-button")
  case containerElement of
    Nothing -> ???
    Just element -> runUI myButton unit element

I don't know what code to put in the Nothing clause so that my code type checks and do nothing in that case.

luis-fdz
  • 161
  • 1
  • 7

1 Answers1

1

pure unit is the "do nothing" you could put in. You can also use for_ to make this a little nicer:

for_ containerElement \element ->
  runUI myButton unit element

Which, if you take currying into account is the same as:

for_ containerElement (runUI myButton unit)
Christoph Hegemann
  • 1,434
  • 8
  • 13
  • None of the solutions ("pure unit" or the "for_" one) compile. They both give me the error: `Could not match type { query :: forall a. Query a -> Aff ( avar :: AVAR, ref :: REF, exception :: EXCEPTION, dom :: DOM | t2) a, subscribe :: FreeT (Await Message) (Aff ( avar :: AVAR, ref :: REF, exception :: EXCEPTION, dom :: DOM | t2)) Unit -> Aff ( avar :: AVAR, ref :: REF, exception :: EXCEPTION, dom :: DOM | t2) Unit} with Unit` – luis-fdz Jun 03 '17 at 16:07
  • 1
    There are two `for_`s in purescript. The one from Control.Safely gives the error above. But with the one from Data.Foldable the code compile and seems to be working pertfectly. Thanks. – luis-fdz Jun 03 '17 at 16:23
  • Another expression that works for the "do nothing" part is `Data.Monoid.mempty`. – luis-fdz Jun 07 '17 at 16:57