Search code examples
elmelm-signal

How to get the current value of a Signal in Elm?


Is there a way to get the current value of a given signal? Or, is this something that I shouldn't want to do when writing idiomatic Elm?


Solution

  • Normal code

    You shouldn't want to do that when writing idiomatic Elm.
    It's also not possible to get the current value of a signal. This would be a side-effecting function (returning different values at different times in the program execution), which would allow all kinds of nasty bugs to crop up. To do something with the value on a signal, you can map over the signal with Signal.map but I suspect you already know that one.

    Testing

    If you're asking about this for testing purposes rather than normal code, you can hack around the limitation using the technique that's used in package Apanatshka/elm-signal-extra to write tests for signal-related functions. (Note that although I'm the author of that package, kudos for the testing system should go to rgremple for conceiving of and contributing it)