7 ms·
Depends what you mean by “custom types.” Elixir supports structs, but they’re not really as rich as classes in another language, for example. If you just want t
by jolux 5y ago
Depends what you mean by “custom types.” Elixir supports structs, but they’re not really as rich as classes in another language, for example. If you just want to write type signatures for your functions though, this is a pretty good guide: https://hexdocs.pm/elixir/1.12/typespecs.html https://hexdocs.pm/elixir/1.12/typespecs.html
- arthurcolle 5y agoRight, using structs for things I'm doing have been extremely painful, to say the least. Let's say I'm trying to create a module that represents a European option and I want to encapsulate things like "strike price", "expiration date", and the various Greeks in a way that are subsequently modifiable (FWIW, it would be perfectly fine to then just respawn a new proc to have the new data if that is absolutely necessary...). I haven't been able to identify a way to even reference other PIDs that exist which might be able to (with some wizardry) solve some of these problems. Right now I'm basically wrapping basically all my data in Agents with maps inside, but that is so lame. Thanks for the quick response. Maybe I'm trying too hard to use a saw to hammer a nail. EDIT: I don't care about type signatures on functions. I'd just like to be able to have a real time system that updates based on ingested values, that I can distribute easily. It seems like it's possible, even close, but the structs just don't get me quite where I want them to be.
- deleted 5y ago[deleted]
- jkhdigital 5y agoYour description is a bit hard to follow, but it smells kind of like something Ecto schemas could deal with? That is, by defining an Ecto schema for your data structure you can go almost directly from raw json to clean, correctly casted data. To use an example similar to yours, this is how I process data from cryptocurrency exchange APIs—just write up a schema for the shape of the response, parse the json into a map and throw the map into Ecto.Changeset.cast. Even if everything is a string in the raw data, it will be correctly cast into integer/float/whatever as defined in the schema.
- arthurcolle 5y agoSorry about that. Vacationing in Miami Beach right now so I might not be 100% in my descriptive capacity. Anyways, yeah, that obviously could work, but I'm kind of trying to make this all just run in memory because I'm already persisting all the data separately.
- jkhdigital 5y agoYou can use Ecto schemas and changesets without a database! It is an incredibly useful library, once you get over the somewhat steep learning curve.
- jolux 5y agoHuh, TIL. Will have to look into that. Do you have any blog posts handy that explain this?
- dnautics 5y agohttps://speakerdeck.com/gvaughn/ecto-without-a-db-many-meanings-of-map https://speakerdeck.com/gvaughn/ecto-without-a-db-many-meani...
- jolux 5y agoThanks!
- arthurcolle 5y agoThanks for your reply to my original comment. I hadn’t considered Ecto, but I guess it does make sense to think of the ChangeSet abstraction irrespective of whether or not you are persisting the data in question. Appreciate it. Do you have an email or something so we could chat further?