Generic conversion between signed and unsigned integer types

Would this solution work for you?

func foo<T: UnsignedInteger>() -> T {
    return 0 // Simplified example :)
}

protocol BitPatternInitializable {
	associatedtype BitPattern: UnsignedInteger
	init(bitPattern: BitPattern)
}

// every type you want to work with
extension Int: BitPatternInitializable {}
extension Int16: BitPatternInitializable {}

func foo<T: BitPatternInitializable>() -> T {
    return T.init(bitPattern: foo())
}
1 Like