Code Monkey home page Code Monkey logo

easy-cal-swift's Introduction

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.

easy-cal-swift's People

Contributors

onevcat avatar honghaoz avatar

Watchers

Lucas Correa avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.