Onilton Maciel Onilton Maciel - 1 month ago 8
Scala Question

Human readable size units (file sizes) for scala code (like Duration)

Are there any libraries that provide an object/class with implicits conversions (from Int, Long, Float) for human readable file size units (like Duration).

With

Duration
you can do this:

11.millis
1.5.minutes
10.hours


I wonder if there is some library that would allow me to do:

1.gibabyte
1024.megabytes
10.gibibytes
10.GB
50.GiB


I know I could implement this myself, but I'm trying to not reinvent the wheel.

Answer

I've just stumbled upon squants. As stated in their own site:

Squants is a framework of data types and a domain specific language (DSL) for representing Quantities, their Units of Measure, and their Dimensional relationships. The API supports typesafe dimensional analysis, improved domain models and more. All types are immutable and thread-safe.

With squants you can do:

10.kib
10.kibibytes
50.mib
100.gib

Although i didn't like that the unit symbols are all lowercase (i.e. gib instead of GiB)