Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> not part of the IntegerType protocol, when it should be

> Submit these as bugs to Apple.

Language designers keep making this mistake. .Net has this problem. Supposedly a bug was submitted for it and it's not possible without breaking some backwards compatibility. Luckily Swift is beta (right?).

Either way, how I solve this "safely" in .Net:

1. Never do bit operations against signed integers. The behavior for this varies wildly across languages, it best to just avoid this altogether.

2. A UInt64 is bit-identical to the Int64 that it was cast from.

I'm guessing at the Swift syntax, but those concepts translate to:

    func integerWithBytes(bytes:[UInt8]) -> UInt64? {
	    if (bytes.count < sizeof(T)) {
		    return nil
	    }
	    for (var j = 0; j < sizeof(T); j++) {
	        i = i | (bytes[j] << (j * 8)) // error!
	    }
        return i
    }
That's right: you simply don't need a generic method.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: