4 ms·
No, they imply that the handling is done by returning a negative number. I'm being snarky, as is my nature, to highlight the madness of a function called `getc
by nixpulvis 7y ago
No, they imply that the handling is done by returning a negative number.
I'm being snarky, as is my nature, to highlight the madness of a function called `getchar` returning anything but a `char`.
- samatman 7y agoIt's not a great snark given that the C standard considers the signedness of char to be implementation defined, making -1 a valid option, sometimes.
- nixpulvis 7y agoI'm sorry you don't find it great (I still do). Integers are not characters. Integers are numbers like -1337, 0, and 42. Characters are things that compose strings of text. These are not the same kind of thing at all. Just because APIs may be leaky, and some of these APIs are held in very high regard doesn't change that fact.
- samatman 7y agoa char isn't a character, though. you can't add two characters together and get another character. it's a number. getchar() gets a char. not a character.
- nixpulvis 7y agoAll you've convinced me of is that the + operator isn't defined for characters. Which makes sense. Trying to tell me that something called char is really just a byte in disguise (while true in some popular languages) is just irritating and misleading to me.
- astrobe_ 7y agoIn the end, integers, floating point numbers, "text\n", emojis etc. are just sequences of bytes. You choose to acknowledge it and take advantage of it, or you don't.
- nixpulvis 7y agoBy that argument, why even have types... and what makes bytes so special? Perhaps you'd like to work with bitstreams, (or qbitstreams)?
- astrobe_ 7y agoA bit late, but bytes are very often the smallest addressable unit. That's why they are "special". Which answers your second question: "bitstreams" would be terrible because they are not well connected with a hardware reality. Unless you have bitstream-oriented CPU, it is a bad idea for a basic type to go against the hardware. Why even have types... Well, yes, there are languages without type checking where the notion still exists. For instance Forth has no type checking but two types are implied: the "byte" type and the "machine word size" type, maybe three if you count strings.
- chrisseaton 7y ago> the madness of a function called `getchar` returning anything but a `char` It’s effectively returning a Maybe(char).
- nixpulvis 7y agoBut it's not. A `Maybe<char>` has exactly one `None` variant. While an `int` has many, many negative values. Also, just calling it `None` (or similar) makes clear what is meant, while `-1` is some magic value.
- chrisseaton 7y ago> while `-1` is some magic value It's a documented return value. Nothing magic about it.