Fl4m3Ph03n1x

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?

Showing Posts 1 to 10

hst337

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

Fl4m3Ph03n1x OP

So what I am asking here is impossible?
Are there any alternatives to this?

hst337

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 write float() with a comment next to it (like float() # from 1.0 to 2.0) without any significant impact on type checking.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

I decided to use float and guards in 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

Fl4m3Ph03n1x OP

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

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

jhogberg

Erlang Core Team

This has nothing to do with dependent types and it would be trivial for dialyzer and other tools to support this. If we were so inclined we could even support things like :foo .. :bar to cover the range of all atoms from :foo to :bar, or why not {any(), 53, float()} .. {12, atom(), map()} and other arbitrary ranges? There’s nothing stopping us from doing so.

The problem is that Dialyzer’s type system doesn’t.

dialyzer does 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 @type annotations (@value_constraint would have been a better name) is nothing more than a limitation in the current implementation of the “type language.”

hst337

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

Fl4m3Ph03n1x OP

Can you elaborate on why a function that returns @type percentage :: 0.0..1.0 is not a function returning a dependent type?

From the wikia:

(…) In computer science and logic, a dependent type is a type whose definition depends on a value [in addition to its type] (…)

This makes sense to me. The percentage() type, were it to exist, would depend not only on parameter being a float but also on the value of said float to be between 0.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

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 flatmap implementation, while for bigger sizes they use hashmap implementation. So, in this example the type depends on the size of the map

While 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 uint is a subtype of int (without maximum integer limit of course), which bounds the integer to always be not less than 0

Where Next? Top

Trending in Questions Top

katta
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
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
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
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 Top

GenericJam
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
JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews