docker-archive / go-p9p

A modern, performant 9P library for Go.
Apache License 2.0
206 stars 50 forks source link

Most QType constants aren't actually of type QType #22

Closed dominikh closed 8 years ago

dominikh commented 8 years ago

In the following piece of code

// Constants for use in Qid to indicate resource type.
const (
    QTDIR    QType = 0x80 // type bit for directories
    QTAPPEND       = 0x40 // type bit for append only files
    QTEXCL         = 0x20 // type bit for exclusive use files
    QTMOUNT        = 0x10 // type bit for mounted channel
    QTAUTH         = 0x08 // type bit for authentication file
    QTTMP          = 0x04 // type bit for not-backed-up file
    QTFILE         = 0x00 // plain file
)

only the first constant has type QType. The other constants are untyped/will default to int when used. Constants only "inherit" the previous constant's type if no explicit value is assigned.

thaJeztah commented 8 years ago

@dominikh would you be interested in opening a pull request for this?

stevvooe commented 8 years ago

Constants actually get type from their context if no type is declared. If the context doesn't define the type, they will default to whatever the default type is for the constant format, which is int in this case.

Were you having particular errors during compilation from this or is this just clean up?

dominikh commented 8 years ago

@stevvooe That's correct. Which means that code like this:

typ := p9p.QTFILE
if someCondition {
  typ = p9p.QTDIR
}
fn(typ)

will fail to build. Similarly, p9p.QTDIR.String() will work, while p9p.QTFILE.String() will not.

Either all or none of the constants should have a type, anything else is inconsistent.