benleggiero
07/15/2018, 9:03 PMInt64Size
vs `Int8Size`:
https://github.com/BlueHuskyStudios/Blue-Base/commit/5baf26fb495b29bd9ad4a7b1099d76c9f0c1c163
Just about all the code between these classes is identical, but solely because I can only say either “Use a `Long`” or “Use any implementation of `Number`“, I have to slowly go through every native number type and make separate implementations for these.
If I could just specify “This is a Size
implementation based on a native integer”, I could write it once and let generics fill in the blanks.
Swift did this with a set of 9 interfaces, and it makes it super easy to let the compiler reason about what I’m getting at:
https://developer.apple.com/documentation/swift/swift_standard_library/numbers_and_basic_values/numeric_protocols
https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.mdkarelpeeters
07/16/2018, 10:26 AMbenleggiero
07/16/2018, 4:57 PMkarelpeeters
07/16/2018, 6:21 PMFixedSizeInteger
, can I add them together?benleggiero
07/16/2018, 10:25 PMFixedWidthInteger
is a protocol (interface) in Swift, it has to be used as a generic constraint instead of a raw parameter type. Additionally, its multiplication function requirements, multipliedFullWidth(by:)
, multipliedReportingOverflow(by:)
, and unsafeMultiplied(by:)
all require the right-hand side and return to be of the same type as the receiver. So, in Swift:
func exampleMultiply<T: FixedWidthInteger>(lhs: T, rhs: T) -> T {
return lhs.unsafeMultiply(by: rhs)
}
Perhaps in Kotlin it might look like this:
fun <T: FixedWidthInteger> exampleMultiply(lhs: T, rhs: T) = lhs.unsafeMultiply(by = rhs)
karelpeeters
07/17/2018, 6:56 PM