SwiftAA icon indicating copy to clipboard operation
SwiftAA copied to clipboard

Saturn magnitude is still buggy in some cases

Open alex-vasenin opened this issue 7 years ago • 3 comments

Saturn magnitude is still buggy in some cases:

    let jd = JulianDay(year: 2018, month: 1, day: 1) // Could be any date
    let jupiter = Jupiter(julianDay: jd)
    let saturn = Saturn(julianDay: jd)
    let uranus = Uranus(julianDay: jd)
    let neptune = Neptune(julianDay: jd)
    let gasGiants = [jupiter, saturn, uranus, neptune]
    print(saturn.magnitude.value) // Prints 0.479, as expected
    print(gasGiants[1].magnitude.value) // Prints nan !!!

This is one of the very few cases where Swift language itself falls short. Saturn's object is both Planet and Saturn. Planet has it's own magnitude as default implementation of IlluminatedFraction protocol. Saturn has it's own magnitude as computed property. Due to static dispatch Swift calls wrong implementation for magnitude in some cases. More info in SR-103.

alex-vasenin avatar Jan 02 '18 17:01 alex-vasenin

Thanks Alexander, you're really up-to-date with these issues I have a hard time to follow. I remember wondering whether a "filling-empty-space" default implementation would be a good idea or not. It is not. I am focused on other things right now. If you have an idea on how to get around this problem... well, of course, I'm sure you'll submit it here. Thanks again!

onekiloparsec avatar Jan 03 '18 08:01 onekiloparsec

Just checked right now, and this is not yet fixed. Damn.

I would like to keep the default implementation. The solution I see for now is to switch from a var to a func. But this will break APIs.

Oups, no. Funcs won't work either, according to https://bugs.swift.org/browse/SR-103

onekiloparsec avatar Feb 01 '19 10:02 onekiloparsec

print((gasGiants[1] as! Saturn).magnitude.value) works, but this is not satisfactory.

onekiloparsec avatar Feb 01 '19 10:02 onekiloparsec