Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/onevcat/Easy-Cal-Swift
Overload +-*/ operator for Swift, make it easier to use (and not so strict)
https://github.com/onevcat/Easy-Cal-Swift
Last synced: 3 months ago
JSON representation
Overload +-*/ operator for Swift, make it easier to use (and not so strict)
- Host: GitHub
- URL: https://github.com/onevcat/Easy-Cal-Swift
- Owner: onevcat
- License: mit
- Created: 2014-06-05T03:30:24.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-05-11T01:21:11.000Z (over 9 years ago)
- Last Synced: 2024-07-31T18:17:44.881Z (6 months ago)
- Language: Swift
- Homepage:
- Size: 162 KB
- Stars: 271
- Watchers: 13
- Forks: 25
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swift-cn - Easy-Cal-Swift - Overload +-*/ operator for Swift, make it easier to use (and not so strict). (Libs / Math)
- awesome-swift - Easy-Cal-Swift - Overload +-*/ operator for Swift, make it easier to use (and not so strict). (Extensions)
README
Easy-Cal-Swift
==============### Overview
This file is an overloading of +-*/ operator for Swift, to make it easier to use (and not so strict)
It can make your life with Swift easier when doing calculation between different type. Swift has a type check and requires left operand and the right one the same type. It causes a lot of trouble that we must convert the type every time.
```
var a = 3
var b = 2.0
a + b //Compile error
a - b //Oops, error again
a * b //Wtf..
a / b //God please save me//You have to write these instead
Double(a) + b
Double(a) - b
Double(a) * b
Double(a) / b
```Yes, it is type safe, but it is also time wasted.
### Usage
Just put the `Easy-Cal.swift` to your project, and you can now have a traditional C-like implicit conversion between `Int`, `Float` and `Double`. So you can write as you would expect without annoying error messages.
```
var a = 3
var b = 2.0
a + b //5.0
a - b //1.0
a * b //6.0
a / b //1.5
````UInt` is also implemented, but it seems there is a bug in this beta seed PlayGround, in which there is a fatal error when you assign an `UInt` value :(. Fortunately, the `UInt` works well in project.
### Risk
Yes, you lose type safe when using these operators, which is opposite to Swift design. But I find at almost all the time, you will know what you are doing with these numbers and the conversion will be just exactly what you want.