Giter Club home page Giter Club logo

clss.extensionmethods.object.is's Introduction

CLSS.ExtensionMethods.Object.Is

Problem

In order to make a type check during a functional-style call chain, you can do it in this way:

using Newtonsoft.Json.Linq;

var resultText = (JToken.Parse(rawJSON).SelectToken(jsonPath) is JObject)
  .ToString().ToUpper(); // resultText: TRUE/FALSE

This syntax breaks the flow of reading code from left to right and makes logical errors from one missing or one extra parenthesis easy to miss. Typical C# programmers are not used to parse parenthesis pairs as Lisp programmers are.

Solution

This package provides Is<T> extension method as a functional equivalence to the is syntax to maintain consistent LTR reading flow and be friendly to the functional syntax. The above first example can be rewritten as follows:

using CLSS.
using Newtonsoft.Json.Linq;

var resultText = JToken.Parse(rawJSON).SelectToken(jsonPath).Is<JObject>()
  .ToString().ToUpper(); // resultText: TRUE/FALSE

It also provides an equivalence to C# 7.0's convenient declaration syntax:

object greeting = "Hello, World!";
if (greeting.Is<string>(out string message))
{ ... }

Due to the limitation in the type system of C#, Is<T> is limited to using reference types. Passing a value type to the type parameter will cause a compilation error. Note that this limitation is strictly for the type parameter. You can still use value types as the caller of Is<T>.

using CLSS;

int number = 5;
var numberIsConvertible = number.Is<IConvertible>(); // true

Is<T> has some boxing overhead. It should be used if said overhead is negligible in your code path and comprehensibility is your priority.

This package is a part of the C# Language Syntactic Sugar suite.

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.