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
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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