Earlier quoted context omitted.
I strongly disagree. The existing getchar() API is not simple at all! All the possible error conditions are still there, they're just obscured by an overstreamlined API which fuses them inappropriately into a single return type. That makes it harder to handle all cases well, because you have to do all the work manually.
The man page for getchar is a single, easy to read paragraph. To understand algebraic types you need a couple of textbooks.
EOF is not a character
111–120 of 136 posts
Re: EOF is not a character
#112Seems like the confusion arises because getchar() (or its equivalent in langauges other than c) can produce an out-of-band result, EOF, which is not a character. Procedural programmers don't generally have a problem with this -- getchar() returns an int, after all, so of course it can return non-characters, and did you know that IEEE-754 floating point can represent a "negative zero" that you can use for an error cod…
What does "Procedural" vs "Functional" have to do with this? It's a choice in data type. If by procedural you mean, nonsense, then sure... I agree that a function named `getchar` returning an `int` is procedural. :P
When Rust introduced ADTs they were recognizably a concept from functional programming. It's a place or community of practice, not a purely descriptive adjective.
Re: EOF is not a character
#113The exception even tells you that "chr() arg not in range(0x110000)" which has nothing to do with range of C's character types.
Re: EOF is not a character
#114Earlier quoted context omitted.
> We can already return structs AFAIK, no? You can return a pointer to a struct, and you can pass whole structs as arguments , but not, IIRC, return them from functions. EDIT: Apparently you can , sort of, but not portably; how exactly it is defined to work depends on the compiler, and each compiler might define it differently. This means that if you’re using a library which returns a struct and your program use a di…
Structs are values and you can return them like any value (or use them as parameters). I'm not sure what you mean about compilers.
Re: EOF is not a character
#115Earlier quoted context omitted.
The man page for getchar is a single, easy to read paragraph. To understand algebraic types you need a couple of textbooks.
No, encoding additional information in unused bits of an int that you return is stupid over-engineering that needs multiple textbooks to grok. Option , on the other hand, is the simplest possible solution for this problem.
Re: EOF is not a character
#116Earlier quoted context omitted.
> We can already return structs AFAIK, no? You can return a pointer to a struct, and you can pass whole structs as arguments , but not, IIRC, return them from functions. EDIT: Apparently you can , sort of, but not portably; how exactly it is defined to work depends on the compiler, and each compiler might define it differently. This means that if you’re using a library which returns a struct and your program use a di…
Structs are values and you can return them like any value (or use them as parameters). I'm not sure what you mean about compilers.
But structure type return values are well specified for most calling conventions, and quite a number of compilers support explicitly specifying the calling convention for mixed-language or mixed-compiler situations.
Re: EOF is not a character
#117Earlier quoted context omitted.
> the madness of a function called `getchar` returning anything but a `char` It’s effectively returning a Maybe(char).
But it's not . A `Maybe ` 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.
It's a documented return value. Nothing magic about it.
Re: EOF is not a character
#118The kernel returns EOF "if k is the current file position and m is the size of a file, performing a read() when k >= m..." So, is the length of each file stored as an integer, along with the other metadata? This reminds me of how in JavaScript the length of an array is a property, instead of a function that counts it right then, like say in PHP. Apparently it works. I've never heard of a situation where the file size…
Re: EOF is not a character
#119Earlier quoted context omitted.
I’m a community college drop out and have never taken a CS class and I use options in my code all the time. It’s just a wrapper around some value that is either Some(value) or None and you need to unwrap it and handle both possibilities for your code to compile. You don’t need to know anything about monads or ADT’s to understand it.
But do you understand that some values of a 32 bit int can be used to encode meta-data about a 8 bit char? If you understand both, which one is conceptually easier to you?
I just do not understand what the problem with Option is.
It's either Some(1) or None. If it's some you have the value, otherwise you handle the fact you don't have it.
It's so simple and basically every modern language uses it to handle nullable types.
Re: EOF is not a character
#120Earlier quoted context omitted.
What always annoyed me about C is that it has all the tools to simulate something approaching this, save for some purely syntactical last-mile shortcomings. We can already return structs; if only there were a way to neatly define a function returning an anonymous struct, and immediately destructure on the receiving end. Something like: #include struct { int err; char c; } myfunc() { return { 0, 'a' }; } int main(int…
Sounds like you'd like Go, which works this way.