builtins.txt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. There is a logic to the set of builtin types.
  2. First, if a type can reasonably be assumed to be provided directly in
  3. hardware, it's a builtin. This covers the 2^N-bit 2s-complement
  4. signed and and unsigned integral types, for N <= K. In the rust 1.0
  5. language standard, K is 7: the set of 2s-complement types are:
  6. u1, u2, u4, u8, u16, u32, u64, u128
  7. s1, s2, s4, s8, s16, s32, s64, s128
  8. Though all s\d+ and u\d+ type names are reserved by the language, in
  9. the language-standard module.
  10. A specific interface for binary floating point number systems exists,
  11. based on the terminology of ISO10967 (language independent
  12. arithmetic). Namely an interface that describes the parameters of a
  13. floating point system, including a boolean flag indicating whether it
  14. conforms to IEEE754 or not.
  15. A module satisfying this interface is bound to the name "floating" in
  16. the language standard module, and the type name "flo" is an alias for
  17. floating.t. Other modules, possibly with hardware support, may exist
  18. under the modules IEEE754 or IEEE754R, but this is implementation
  19. dependent. The interface provided by "floating" is the
  20. highest-precision binary floating point type available on a given
  21. platform.
  22. A general "dec" type is also present in the language. This is *not*
  23. any of the decimal types with limited precision available in IEEE854 /
  24. IEEE754R; it is an arbitrary-precision decimal type (2 bignums: a
  25. significand and an exponent) and arithmetic operations on it are
  26. well-defined, slow, portable and pure.
  27. At a language level, a lexeme is reserved for the dec type -- the
  28. lexeme that has scientific notation input and/or decimal points -- and
  29. a conversion exists to most other numeric types. Dec operations may be
  30. compiled out, but not flo operations.
  31. The standard module defines a set of convenience type aliases for
  32. these: word, sword, flo and dec. bit is an alias for u1 and byte is an
  33. alias for u8. signed bytes are an abomination.
  34. (note that machine alignment rules may cause multibyte slots to align
  35. and pad their container type)
  36. Second, if a type has one of a handful of literal forms that we want
  37. to support "unadorned" in the text, we need to support parsing and
  38. initializing types denoted by the literal form. So for example "str"
  39. and "char" are builtin, even though they model alts / vecs of integral
  40. types; the compiler nonetheless needs to *build* those when it sees
  41. str/char literals, and it needs to spit them out in a human-digestable
  42. form when reflecting and pretty-printing. Similarly the
  43. arbitrary-precision int and dec types are builtin because they must be
  44. initialized from integral / decimal literals, and print back to them.
  45. The tilde operator exists as an escape hatch for new types that you
  46. wish to give literal support to. The dividing line between the handful
  47. of types we want to provide builtin literal support for and those we
  48. wish to leave for the tilde operator is a matter of taste; we have
  49. erred on the side of conservatism and only included a small number:
  50. number and string types (plus algebraic types built of them). When a
  51. tilde-expander registers with the compiler it can also register a
  52. pretty-printing helper against any types it wishes to handle printing.