OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:20 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 39 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: Does my keyboard.h work?
PostPosted: Wed Nov 16, 2016 10:14 am 
Offline
Member
Member

Joined: Sun Feb 01, 2009 6:11 am
Posts: 1070
Location: Germany
alexfru wrote:
OTOH, enums by default fit into signed ints and if an enumeration constant doesn't fit into a signed int you should get a compilation error.

IIRC, enums are always ints, but the signedness is implementation defined. In the common case (gcc on x86 and Linux at least), you get unsigned unless you explicitly assign a negative value to one of the constants. That's why you tend to get warnings when x is some enum variable and you try to check for x < 0 before using it as an array index. But of course, the check isn't really redundant, unless you know you'll always build with exactly the same compiler. But I'm digressing...

_________________
Developer of tyndur - community OS of Lowlevel (German)


Top
 Profile  
 
 Post subject: Re: Does my keyboard.h work?
PostPosted: Wed Nov 16, 2016 10:49 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
Yes, but as I said earlier, if you are using an enumeration to define constants without regard to order, you're Doing It Wrong. The purpose of enum is to define an ordering; that it does so by defining constants is a side effect, not the purpose of the construct. Some languages which support enumerations (e.g., Ada) don't allow you to directly access the underlying constant values for this very reason (they usually provide an accessor function such as ord() if you absolutely must have the enumeration of a specific enumeration constant).

Oh, and C has a const keyword too, now, and has for decades. It may not work quite as you would expect, but it is there, and unlike #define, it does specify type.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: Does my keyboard.h work?
PostPosted: Wed Nov 16, 2016 9:18 pm 
Offline
Member
Member
User avatar

Joined: Thu Mar 27, 2014 3:57 am
Posts: 568
Location: Moscow, Russia
I never heard that enums define something that must be ordered. To me it seems pretty logical to use them for related things. Isn't it a common practice to use them for completely unordered error codes?

_________________
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay


Top
 Profile  
 
 Post subject: Re: Does my keyboard.h work?
PostPosted: Wed Nov 16, 2016 10:52 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
Common? Yes. Sensible? Not hardly.

Well, not to me, anyway. YMMV.

It is sort of like arguing that using fflush() to flush* input streams (which shouldn't even work, though many badly broken implementations allow it) is the right thing to do because it is a common approach. The fact that fflush() should only be used for, should only work for, output streams is still an important point, because that is what it was intended for and really only makes sense for.

Breaking the model out of pragmatism is one thing, breaking it out of laziness or convenience is another.

It does seem that people have forgotten that this was the original idea**, though, and a lot of (kinda-sorta) working code uses it, so maybe I'm the crazy one here. After all, we all know I am mad anyway, so this would be just one more thing I am insane regarding.

1) Before Unix ruled the world, flushing a buffer meant dumping its contents, not forcing it to go to completion. Who decided that calling the 'force output to completion' function fflush() was a good idea? (Dennis Ritchie, apparently, which is one more thing for which he deserves the blame.)

2) the enum keyword was a relatively late addition, being introduced in the ANSI standard in 1989. It was something of an afterthought, meant to appease the Ada and Pascal fans who were being force to use C as it became an all-consuming juggernaut, but the intention was thought to be clear from the name. Apparently not.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: Does my keyboard.h work?
PostPosted: Thu Nov 17, 2016 7:42 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 10, 2016 7:35 am
Posts: 167
Location: Lancaster, England, Disunited Kingdom
Schol-R-LEA wrote:
the enum keyword was a relatively late addition, being introduced in the ANSI standard in 1989. It was something of an afterthought, meant to appease the Ada and Pascal fans who were being force to use C as it became an all-consuming juggernaut, but the intention was thought to be clear from the name. Apparently not.[/i]


I can't speak for ADA but in Pascal the idea was far more concerned with the internal semantics of the programme being visible in the code. Thus, you would have types such as:

Suit=(Clubs, Diamonds, Hearts, Spades)
Season=(Spring, Summer, Autumn, Winter)
House=(Coopers, Maples, Mellers, Whites)
FileSystem=(EXT2, EXT3, EXT4, FAT12, FAT16, FAT32, NTFS)

The first two do have some sort of ordering idea, but the last two don't apart from being in a conventional alphabetic order
The motivation, however, is

(a) to make the code reflect what is being done (the ultimate ideal would be for the code to be self-documenting):

if (DriveFileSystem==FAT12) {
print("Time to upgrade your system, mate")
}

(b) to make it difficult to assign values that are meaningless to the program:

DriveFileSystem=5
Error: Cannot assign integer to variable of type 'FileSystem'

or

DriveFileSystem=Hearts
Error: Hearts is not a FileSystem.

That's not to say that the ordering was not both used and useful, but I think it is wrong to regard it as a prime reason for the construct.


Top
 Profile  
 
 Post subject: Re: Does my keyboard.h work?
PostPosted: Thu Nov 17, 2016 8:32 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
Fair enough. As I said, however, enumerations are a special case of that kind of discrete-valued type, and the lack of discrete-valued types of a more general sort in C, and the fact that the language exposes the underlying implementation, are why we tend to see enumerations as defining valued constants rather than an ordering or a set of discrete data points.

Still, at this point I will concede that I have been making too much of the issue of ordering. I have been a bit caught up in the idea of enumerations being for enumerating, and I'll admit that using them for defining constants isn't actually a bad thing in and of itself. I still would say that their primary purpose is to define a set of discrete identities, and I don't dispute that using them for defining constants is useful, but... well, I can't help but feel that this is as much a matter of the lack of richness in the language than it is of programmers misusing something.

Probably it's because I've gotten used to the idea that a language can define new forms of data construct- not just new types or classes, but new kinds of types and ways of defining them - something which is a pretty basic thing in Lisps. Once you have lexical macros (especially read macros) and a self-consistent base syntax, the difference between 'core language' and 'library' pretty much disappears from the perspective of the client-programmer. I certainly intend to to have a lot of different kinds of domains in the standard library for Thelema (if I ever get around to developing it), including things like different kinds of enumerations, sets, ranged types, ordered and unordered collections, and so forth, some of which will have their own literal syntax defined for them. The core language will be tiny - smaller even than Scheme, but with a lot more focus on the programming tools such as package management, scoping rules, generic programming, compiler-level extensibility, dynamic selection of automatic memory management models, and controlling the interface with the system - but I hope to eventually build a large suite of libraries to go with it.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Last edited by Schol-R-LEA on Sat Nov 19, 2016 12:09 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Does my keyboard.h work?
PostPosted: Sat Nov 19, 2016 3:33 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
Kevin wrote:
IIRC, enums are always ints, but the signedness is implementation defined.


C99 says in 6.7.2.2 Enumeration specifiers:
The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.

So, no unsigned int. Can probably be unsigned char/short if converts into signed int (I'd need to check for this), but even so, in all expressions, except sizeof, it would behave as a signed int because of the default conversions applied to operator operands.


Top
 Profile  
 
 Post subject: Re: Does my keyboard.h work?
PostPosted: Sat Nov 19, 2016 7:17 am 
Offline
Member
Member

Joined: Sun Feb 01, 2009 6:11 am
Posts: 1070
Location: Germany
Paragraph 2 is only about the allowed values for the enum constants, it doesn't say anything about whether a variable of the enum type is signed or unsigned. For the latter question, paragraph 4 is relevant: "Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration." So unless you have negative enum constants, an enum variable can be treated as unsigned, and I'm pretty sure that gcc does this.

_________________
Developer of tyndur - community OS of Lowlevel (German)


Top
 Profile  
 
 Post subject: Re: Does my keyboard.h work?
PostPosted: Sat Nov 19, 2016 4:13 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
Kevin wrote:
Paragraph 2 is only about the allowed values for the enum constants, it doesn't say anything about whether a variable of the enum type is signed or unsigned. For the latter question, paragraph 4 is relevant: "Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration." So unless you have negative enum constants, an enum variable can be treated as unsigned, and I'm pretty sure that gcc does this.


You're right.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 39 posts ]  Go to page Previous  1, 2, 3

All times are UTC - 6 hours


Who is online

Users browsing this forum: DotBot [Bot] and 34 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group