3 ms·
You can already do this func die(lowerBound: Int, upperBound: Int) -> () -> Int { { (lowerBound...upperBound).randomElement()! } } let d6
by edflsafoiewq 7y ago
You can already do this
func die(lowerBound: Int, upperBound: Int) -> () -> Int {
{ (lowerBound...upperBound).randomElement()! }
}
let d6 = die(lowerBound: 1, upperBound: 6)
let roll1 = d6()
so the syntax already exists.
- lostmsu 7y agoBut what if you want to do something apart from rolling with a die?
- edflsafoiewq 7y agoThen you can create a struct Die and change die to return that. By implementing callAsFunction you keep the same interface as before so existing callers don't break.
- jayd16 7y agoAha ok. This makes far more sense actually. Its a refactor tool for replacing lambdas with callable objects. But does it really work out? Surely you can't put these new structs into the same arrays as similar lambdas, can you?