Coder Social home page Coder Social logo

uniontype-java's Introduction

UnionType for The Java Programming Language

1. What is union type?

you can see some details in the Typed-Racket Programming Language:

#lang typed/racket
(define-type Tree (U leaf node))
(struct leaf ([val : Number]))
(struct node ([left : Tree] [right : Tree]))
 
(: tree-height (-> Tree Integer))
(define (tree-height t)
  (cond [(leaf? t) 1]
        [else (max (+ 1 (tree-height (node-left t)))
                   (+ 1 (tree-height (node-right t))))]))
 
(: tree-sum (-> Tree Number))
(define (tree-sum t)
  (cond [(leaf? t) (leaf-val t)]
        [else (+ (tree-sum (node-left t))
                 (tree-sum (node-right t)))]))

2. Use this library

note: you can see the tests in test of this project for more details.

I. basic usage

Union strOrInt = new Union(String.class, Integer.class, Null.class);
/* error: don't init the value */
// strOrInt.get(String.class);
/* set strOrInt to Integer 32 */
strOrInt.set(32);
/* reset strOrInt to Integer 132 */
strOrInt.set(132);
/* reset strOrInt type to String type */
strOrInt.set("hello");
/* error: don't specify type LinkedList */
// strOrInt.set(new LinkedList<>());
if (strOrInt.isType(Integer.class)) {
    System.out.println("number: " + strOrInt.get(Integer.class));
} else if (strOrInt.isType(String.class)) {
    System.out.println("string: " + strOrInt.get(String.class));
} else {
    System.out.println("other type");
}

II. with the Null operation

/* 1. use `Union` class to manipulate nullable instance */
Union nullableStr = new Union(String.class, Null.class);
nullableStr.set(Null.instance);
if (nullableStr.isType(Null.class)) {
    System.out.println("nullableStr is null");
} else {
    System.out.println("nullableStr: " + nullableStr.get(String.class));
}
/* 2. or simplify use the `Option` class */
Option canNullStr = new Option(String.class);
if (canNullStr.isNull()) {
    System.out.println("canNullStr is null");
} else {
    System.out.println("canNullStr: " + canNullStr.get(String.class));
}

uniontype-java's People

Contributors

davidquan111 avatar

Stargazers

 avatar

Watchers

 avatar  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.