dimitarvp
Can we import types and guards from another module?
Given this module:
defmodule A do
@type size :: pos_integer()
defguard is_size(x) when is_integer(x) and x > 0
def default_size(), do: 1
end
…how do we make the below work without prefixes?
defmodule B do
@type t :: %__MODULE__{size: size()}
defstruct [size: default_size()]
def put_size(opts, size) when is_list(opts) and is_size(size), do: Keyword.put(opts, :size, size)
end
Tried import A and require A, both don’t get the job done.
What elementary Elixir lesson I managed to forget?
Marked As Solved
NobbZ
You should be able to import is_size/1 just by doing import A(, only: [is_size: 1]).
You can not import types though. The easiest way is to just alias them.
@type foo :: Foo.foo
15
Also Liked
RobertDober
You might also group your types into a module like so
defmodule MyApp.Types do
defmacro __using__(_opts) do
quote do
@type greek_t :: :alpha | :beta
@type mystruct_t :: MyApp.MyStruct.t # Beware of circulars here!!
end
end
...
9
Popular in Questions
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
I would like to know what is the best IDE for elixir development?
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New







