Fl4m3Ph03n1x
Background
I want to create a percentage type using floats, something like:
@type percentage :: 0.0..1.0
Now, you will know that this is not a valid typespec, because I am using 0.0.
The (incorrect) fix there would be to change the spec to:
@type percentage :: 0..1
But this now means I have a range of integers from 0 to 1. I do not want integers, I want floats.
I have also checked the Range.t() option, but this is not what I want. My API requires floats, not an object of type Range.
Question
How can I fix my spec so it works?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hst337
You can’t have float range in typespecs. There is no such expressions by design.
Don’t forget to mark the correct answer
Fl4m3Ph03n1x
So what I am asking here is impossible?
Are there any alternatives to this?
hst337
Yes, it is impossible in current typespecs language. Dialyzer will reduce complexity of every integer range typespec into
integer()during checking (that’s how dialyzer works). So you can just writefloat()with a comment next to it (likefloat() # from 1.0 to 2.0) without any significant impact on type checking.Fl4m3Ph03n1x
I decided to use
floatandguardsin the function. It is not the ideal situation, but its better than having no checks at all.For those of you reading only the solution, @hissssst provided detailed reasons as to why the solution I wanted to achieve is not possible (see How to typespec a float range in Elixir? - #4 by hst337)
Fl4m3Ph03n1x
For a more detailed explanation, there is this SO answer:
Basically, this all comes down to the type I am trying to create - a dependant type (a type that depends both on the value and type of the parameter). Because here the range of possible values is infinity, this is not possible.
hst337
Some type systems have infinite ranges of values. The problem is that Dialyzer’s type system doesn’t. And no, this type is not dependant. The author of SO answer is completely wrong
jhogberg
This has nothing to do with dependent types and it would be trivial for
dialyzerand other tools to support this. If we were so inclined we could even support things like:foo .. :barto cover the range of all atoms from:footo:bar, or why not{any(), 53, float()} .. {12, atom(), map()}and other arbitrary ranges? There’s nothing stopping us from doing so.dialyzerdoes not have a “type system,” it’s untyped and only reasons about sets of possible values (which may be infinite). That you cannot express arbitrary sets of possible values through@typeannotations (@value_constraintwould have been a better name) is nothing more than a limitation in the current implementation of the “type language.”hst337
Yes, I meant expressiveness of type language. But dialyzer has a type system, Type system - Wikipedia, it is just pluggable and gradual without any impact on runtime or performance
Fl4m3Ph03n1x
Can you elaborate on why a function that returns
@type percentage :: 0.0..1.0is not a function returning a dependent type?From the wikia:
This makes sense to me. The
percentage()type, were it to exist, would depend not only on parameter being afloatbut also on the value of said float to be between0.0 .. 1.0.Can you elaborate as to why
percentage()cannot be considered a dependent type?How so? How could I make this work?
hst337
No, dependent type is a type which depends on the value
For example, if Erlang/Elixir was a statically typed language, maps there would be dependent. When their size is less than 33 items, they use
flatmapimplementation, while for bigger sizes they usehashmapimplementation. So, in this example the type depends on the size of the mapWhile for float ranges, it is just a range, and it is not a dependant type. It is just a subtype of the float, and that’s all. Like
uintis a subtype ofint(without maximum integer limit of course), which bounds the integer to always be not less than 0