O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating Structs

We had a pleasant surprise when we built this API. We’d accidentally built in support for structs! Since an Elixir struct is just a map with the extra __struct__ key, just adding the key __struct__: User to the existing User record would create users that satisfy a struct called User like this:

 ~~~
  register :user,
  __struct__: User,
  first_name: Faker.Name.first_name,
  last_name: Faker.Name.last_name,
  email: Sequence.next(:email, &"test#{&1}@example.com")
 ~~~

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required