Most active commenters

    ←back to thread

    182 points Twirrim | 16 comments | | HN request time: 0.205s | source | bottom
    Show context
    WalterBright ◴[] No.41875254[source]
    D made a great leap forward with the following:

    1. bytes are 8 bits

    2. shorts are 16 bits

    3. ints are 32 bits

    4. longs are 64 bits

    5. arithmetic is 2's complement

    6. IEEE floating point

    and a big chunk of wasted time trying to abstract these away and getting it wrong anyway was saved. Millions of people cried out in relief!

    Oh, and Unicode was the character set. Not EBCDIC, RADIX-50, etc.

    replies(3): >>41875486 #>>41875539 #>>41875878 #
    1. cogman10 ◴[] No.41875539[source]
    Yeah, this is something Java got right as well. It got "unsigned" wrong, but it got standardizing primitive bits correct

    byte = 8 bits

    short = 16

    int = 32

    long = 64

    float = 32 bit IEEE

    double = 64 bit IEEE

    replies(2): >>41875597 #>>41875634 #
    2. jltsiren ◴[] No.41875597[source]
    I like the Rust approach more: usize/isize are the native integer types, and with every other numeric type, you have to mention the size explicitly.

    On the C++ side, I sometimes use an alias that contains the word "short" for 32-bit integers. When I use them, I'm explicitly assuming that the numbers are small enough to fit in a smaller than usual integer type, and that it's critical enough to performance that the assumption is worth making.

    replies(3): >>41875695 #>>41875827 #>>41875847 #
    3. josephg ◴[] No.41875634[source]
    Yep. Pity about getting chars / string encoding wrong though. (Java chars are 16 bits).

    But it’s not alone in that mistake. All the languages invented in that era made the same mistake. (C#, JavaScript, etc).

    replies(3): >>41875696 #>>41876204 #>>41876445 #
    4. Jerrrrrrry ◴[] No.41875695[source]
    hindsight has its advantages
    5. paragraft ◴[] No.41875696[source]
    What's the right way?
    replies(2): >>41875771 #>>41875782 #
    6. WalterBright ◴[] No.41875771{3}[source]
    UTF-8

    When D was first implemented, circa 2000, it wasn't clear whether UTF-8, UTF-16, or UTF-32 was going to be the winner. So D supported all three.

    7. Remnant44 ◴[] No.41875782{3}[source]
    utf8, for essentially the reasons mentioned in this manifesto: https://utf8everywhere.org/
    replies(1): >>41875952 #
    8. jonstewart ◴[] No.41875827[source]
    <cstdint> has int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, and uint64_t. I still go back and forth between uint64_t, size_t, and unsigned int, but am defaulting to uint64_t more and more, even if it doesn't matter.
    9. kazinator ◴[] No.41875847[source]
    > you have to mention the size explicitly

    It's unbelievably ugly. Every piece of code working with any kind of integer screams "I am hardware dependent in some way".

    E.g. in a structure representing an automobile, the number of wheels has to be some i8 or i16, which looks ridiculous.

    Why would you take a language in which you can write functional pipelines over collections of objects, and make it look like assembler.

    replies(2): >>41875953 #>>41876035 #
    10. josephg ◴[] No.41875952{4}[source]
    Yep. Notably supported by go, python3, rust and swift. And probably all new programming languages created from here on.
    11. pezezin ◴[] No.41875953{3}[source]
    If you don't care about the size of your number, just use isize or usize.

    If you do care, then isn't it better to specify it explicitly than trying to guess it and having different compilers disagreeing on the size?

    replies(1): >>41875968 #
    12. kazinator ◴[] No.41875968{4}[source]
    A type called isize is some kind of size. It looks wrong for something that isn't a size.
    replies(1): >>41876423 #
    13. Spivak ◴[] No.41876035{3}[source]
    Is it any better calling it an int where it's assumed to be an i32 and 30 of the bits are wasted.
    14. jeberle ◴[] No.41876204[source]
    Java strings are byte[]'s if their contents contain only Latin-1 values (the first 256 codepoints of Unicode). This shipped in Java 9.

    JEP 254: Compact Strings

    https://openjdk.org/jeps/254

    15. pezezin ◴[] No.41876423{5}[source]
    Then just define a type alias, which is good practice if you want your types to be more descriptive: https://doc.rust-lang.org/reference/items/type-aliases.html
    16. davidgay ◴[] No.41876445[source]
    Java was just unlucky, it standardised it's strings at the wrong time (when Unicode was 16-bit code points): Java was announced in May 1995, and the following comment from the Unicode history wiki page makes it clear what happened: "In 1996, a surrogate character mechanism was implemented in Unicode 2.0, so that Unicode was no longer restricted to 16 bits. ..."