Search code examples
purescript

adding to do block, stops code execution


foreign import subscribeEventedOnPrime
  "function subscribeEventedOnPrime(n){  \
  \ return function(fn){                 \
  \    return function(obj){             \
  \       return function(){             \
  \         obj.addEventListener(n, fn); \
  \         return obj;                  \
  \       };                             \
  \     };                               \
  \  };                                  \
  \}" :: forall d a o eff. 
         String -> 
         (d -> a) -> 
         o -> 
         Eff (customEvent :: CustomEvent | eff) o

subscribeEventedOn n f o = subscribeEventedOnPrime n (\e -> do
    trace "wtf" -- if this line is removed, everything seems to work
    f $ newEvent e."type" e."detail"
  ) o

Whether a do block has one line vs more than one line, seems to effect whether or not that code actually gets called. What am I missing?


Solution

  • I think it's because by intoducing the trace you're making d -> a into something like

    forall e. d -> Eff (trace :: Trace | e) Unit
    

    which means it's not going to be evaluated unless you use unsafeInterleaveEff or something similar to actually run it.

    I'm not 100% sure, but maybe the compiler shouldn't let you use the do without the trace at all, I'll have to investigate a bit.