Substitute global variables with Int extensions
dreymonde opened this issue · 4 comments
dreymonde commented
Right now second
, millisecond
, minute
and hour
are just hardcoded Int64
values. As alternative, I propose using Int
extensions to make code slightly more readable and safe:
nap(1.second)
after(5.seconds) {
// Do something
}
It can be achieved this way:
extension Int {
var millisecond: Int64 {
return Int64(self * 1)
}
var milliseconds: Int64 {
return millisecond
}
var second: Int64 {
return Int64(self * 1000)
}
var seconds: Int64 {
return second
}
// and so on
}
Or nicer — with protocol:
public protocol IntervalConvertible {
var millisecond: Int64 { get }
var second: Int64 { get }
// and so on
}
extension IntervalConvertible {
public var milliseconds: Int64 {
return millisecond
}
public var second: Int64 {
return millisecond * 1000
}
public var seconds: Int64 {
return second
}
// and so on
}
extension Int: IntervalConvertible {
public var millisecond: Int64 {
return Int64(self * 1)
}
}
And even now
can has it’s alternative:
extension Int64 {
public var fromNow: Int64 {
return Clibvenice.now() + self
}
}
So one will be able to write:
let timer1 = Timer(deadline: now + 2 * second)
let timer2 = Timer(deadline: 2.seconds.fromNow)
paulofaria commented
this is pretty nice! can you send a PR?
dreymonde commented
@paulofaria sure! Should I remove global variables or leave them?
paulofaria commented
Leave then for now for retro-compatibility. Can you also update the README, please?
dreymonde commented
@paulofaria of course