fireproofsocks

fireproofsocks

Type binary() vs String.t()

When adding a @spec to a function, is there any functional difference between doing something like

@spec my_func(input :: binary()) :: any()

vs

@spec my_func(input :: String.t()) :: any()

Does dialyzer treat those differently? A String is a UTF-8 encoded binary, yes? If you were parsing raw binary data (like zeros and ones from an image file), then I would declare the type as binary, but for regular stuff like email addresses, that would be a String.t(), yes?

Just a reality check, soliciting wisdom from the community. Thanks!

Marked As Solved

champeric

champeric

Dialyzer doesn’t treat them differently. The doc states that they are equivalent to analysis tools and String.t() is defined as @type t() :: binary().

But for clarity, the way you explained you would use them seems right to me.

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement