dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.24k stars 1.57k forks source link

proposal to introduce a rich, efficient and more flexible type system in dart 3.x #51994

Open insinfo opened 1 year ago

insinfo commented 1 year ago

This proposal aims to introduce a rich, efficient, expressive and more flexible type system in dart, for this the language will introduce several types of integers and floats such as 4-bit integer, 8-bit integer, 16-bit integer, among others. The main reason for having different types of numbers is efficiency and accuracy. For example, using smaller integer types (such as int8 and int16) can save memory space in programs that don't need large integer values. Also, using an integer type that exactly matches the size of the value can improve performance by avoiding the need for type conversions. Likewise, using smaller floating-point types (such as float32) can save memory space and improve performance in programs that don't require high decimal precision. On the other hand, the use of larger floating point types (such as float64) is necessary for calculations that require high decimal precision. In summary, having different types of numbers in a programming language allows programmers to choose the right type for their specific needs, thus improving the efficiency and accuracy of their programs.

Integer Types

Floating-Point Types

These numeric data types are designed to have fixed and specific sizes, ensuring program consistency and portability. The signed integer types allow for representation of negative and positive numbers. The floating-point types have different levels of precision, allowing developers to choose the type that is most appropriate for their needs.

These data types should also have full support for mathematical operations, comparison operators, type conversions, and other language features available to existing numeric data types in Dart.

In summary, this preliminary specification for new numeric data types in the Dart language aims to meet developers' needs for numeric data types with different sizes and precisions, improving program efficiency and accuracy.

the current int and double would not change to keep the same platform specific semantics to better map to native machine architecture and better efficiency as well as avoid breaking compatibility

Possible use cases for the int4, int8, int16 and int128 integer types:

int4

In microcontroller applications, where resources are limited and compact data representation is important. In certain cryptographic algorithms, where data can be represented as bit-fields and operations can be performed on smaller bit sizes. In some data compression algorithms, where data can be packed into 4-bit chunks for more efficient storage.

int8

In audio processing applications, where a single audio sample can be represented as an int8 value, with values ranging from -128 to 127 representing the audio amplitude at a given point in time. In computer graphics applications, where an int8 value can be used to represent color values in a color palette, or to store the alpha channel of an image. In embedded systems with limited memory, where an int8 value can be used to save memory when storing small integer values, such as representing button presses or switch states. int16 In GPS applications, where latitude and longitude values can be represented as int16 values with a high degree of precision. In digital signal processing applications, where audio or other signals can be represented as int16 values with higher precision than #### int8. In scientific or engineering applications, where sensor data or other measurements can be represented as int16 values with a high degree of accuracy.

int128

In cryptography applications that require very large integers for key generation and encryption/decryption operations. In scientific or financial applications that require very high-precision arithmetic operations with large integers. In some data storage applications where data elements can be very large, such as large database records or file systems.

this also makes it easier to port code from other languages to dart

I believe the impact will not be very high in terms of breaking backwards compatibility as this is not a feature removal but an addition of new types and optimization.

lrhn commented 1 year ago

I'm fairly sure there is an existing issue about adding types like this. Somewhere.

eernstg commented 1 year ago

@insinfo, you might want to take a look at this issue, and possibly add your proposal there if it isn't already covered by the existing discussion.