Elm: how does a mailbox work?

Let's say I create a Mailbox using mailbox = Signal.mailbox Action , where type Action = Blah , later I can send actions to this Mailbox using Signal.send mailbox.address Blah , this allows me to listen to mailbox.signal , how to do it? I mean, the Mailbox type is just an alias {address : Signal.Address Action, signal : Signal.Signal Action} , because in elms there is only one signal for a certain type, so in the above script I do not need to specify elm to bind mailbox.signal using mailbox.address , will elm find out because of a one-to-one correspondence between an address and a signal of a certain type?

+5
source share
1 answer

The ability of a mailbox to place events that you send into it in your signal is completely based on "magic", that is, it was implemented initially (in JavaScript), and not what you could implement yourself. This is why it is built into standard libraries.

Creating a mailbox is an imperative, effective action (shhh, don't tell anyone). Therefore, if you use:

 mailbox1 = Signal.mailbox Blah mailbox2 = Signal.mailbox Blah 

these two mailboxes will be different. Sending a message to mailbox2.address will result in a message on mailbox2.signal , but not mailbox1.signal . This breaks link transparency, which is bad, but until it breaks everything. (This may go too far into the tangent, but since the output requires Signal and cannot have Signal (Signal something) , in practice the uncontrolled effect of creating a mailbox does not confuse things). This "leak" will be fixed at some point in the future. There was already a proposal, but big changes were made in different versions of the language.

+6
source

All Articles