Angle type
- Dominant language
- Swift
- Stars
- 1.9k
- Forks
- 181
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 6
Description
I wonder if we might rather define our trig functions in terms of an `Angle` type:
```swift
public struct Angle {
public var radians: T
public init(radians: T) { self.radians = radians }
public static func radians(_ val: T) -> Angle { .init(radians: val) }
public var degrees: T { radians * 180 / .pi }
public init(degrees: T) { self.init(radians: degrees * .pi / 180) }
public static func degrees(_ val: T) -> Angle { .init(degrees: val) }
}
```
Browsing [SE-0246](https://github.com/apple/swift-evolution/blob/master/proposals/0246-mathable.md), this alternative design doesn't seem to have been considered, but I think it would help to make our functions more useful and self-documenting. Depending on what you're doing, it can be more natural to think in terms of degrees rather than radians. In C, people tend to bring these utility functions/macros by themselves, whereas other languages like Java [include them](https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#toRadians(double)) as part of the standard library.
The design of the type given above means that creating and consuming an `Angle` in radians in essentially free.
Clearly, we cannot make this change to the `ElementaryFunctions` protocol requirements, as we need the conforming type to provide those implementations. However, we can do this for the free functions - either in addition to, or in place of the regular functions which take a `` argument.
Contributor guide
Research direction
Start by reviewing the ElementaryFunctions protocol and the free trigonometric functions referenced in the issue. Determine whether the proposed Angle type should supplement or replace the existing free-function interfaces; done means the design decision is resolved and the affected public API is implemented with coverage for radians and degrees.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100