Sending Structs in GenServer Calls

65
clicks
Sending Structs in GenServer Calls
The author, mucho_mass, is experimenting with GenServer to create a key-value store in Elixir. They have created a custom struct `FormatFile` and want to use an atom, `:put`, alongside this struct to communicate with GenServer. However, they're facing a syntax error when trying to use `GenServer.cast(server, %FormatFile{:put, timestamp, key, value})`. Other users, ScrimpyCat and bmitc, suggest that the correct approach is to pass the atom and the struct within a tuple, and the correct form of the struct should be `%FormatFile{timestamp: some_timestamp, key: key, value: value}`. Thanks to the community's help, mucho_mass understands the proper syntax for sending the command and struct data together to GenServer.

© HashMerge 2024