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

Blokh
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
kszambelanczyk
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
Onor.io
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
Trolleger
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
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
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
samoloth
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 Top

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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews