Music icon indicating copy to clipboard operation
Music copied to clipboard

Remove Duration runtime constructor errors

Open bwetherfield opened this issue 7 years ago • 6 comments

At present let badDur: Duration = 1/>31 causes an error, because 31 is not a power of 2.

Proposed eradication of these errors: A constructor of the following form.

init(_ numerator: Beats, _ power: Power) {
    self.beats = numerator
    self.denominator = pow(2, power)
}

where Power may be implemented as Int as Subdivision is now.

bwetherfield avatar Oct 09 '18 20:10 bwetherfield

This has the added bonus that technically a breve/double-whole-note is now possible with power = -1...

bwetherfield avatar Oct 09 '18 20:10 bwetherfield

I really like this. I used the other way as it is more immediately intuitive (I personally don't think of rhythmic subdivision values by their power of two). Perhaps for now, we could keep both.

Wanna open a PR which adds:

extension Duration {
    init(_ numerator: Beats, power: Power) { ... }
}

Note: power argument label extant.

~~You will have to add a Power struct which is a wrapper for Int, with a failable (or crashing) initializer. Maybe look into using the NewType protocol in Structure/DataStructures.~~

~~If only Swift had dependent types!~~

Nevermind, misunderstood. This would be an excellent idea! And no new types needed.

jsbean avatar Oct 09 '18 21:10 jsbean

Re: dependent types: very interesting project: https://github.com/silt-lang/silt

jsbean avatar Oct 09 '18 21:10 jsbean

It might actually make things easier down the line if the power was the actual stored value. See ProportionTree, etc. where a bunch of decoding of powers has to take place. Perhaps that could be simplified with this approach.

jsbean avatar Oct 09 '18 21:10 jsbean

Interesting. Yes trying to make sense of it at the moment!

bwetherfield avatar Oct 12 '18 01:10 bwetherfield

ProportionTree that is

bwetherfield avatar Oct 12 '18 01:10 bwetherfield