Giter VIP home page Giter VIP logo

varnum's Introduction

Varnum

Java tagged unions

https://github.com/wehjin/varnum

Overview

Varnum is an annotation processor that generates tagged-union classes, also known as variant-enums or sum-types, for Kotlin, Java, and Android.

An annotated interface specifies the name of the tagged-union along with the names and data-types of its members. The interface begets a tagged-union class, construction functions for each member of the union, and a match function that identifies a member and unwraps its data.

Varnum is 100% generated code with no reflection or byte-code manipulation. You can see the generated classes yourself by viewing the auto-generated tagged-union class after a build.

Usage

Varnum works by building a class for each interface annotated with @TaggedUnion, e.g.

@TaggedUnion("Message")
interface MessageSpec {
    void Reset();
    void SetSize(Integer size);
    void Multi(Integer first, String second);
}

This example generates a tagged-union class named Message which has code to create an instance of each of its members: Reset, SetSize, and Multi. Additional code matches an instance to a member and unwraps the member's data.

Construction takes place like this:

final Message message = Message.SetSize(5);  // Construct a message

Later, you can match a specific message and unwrap its data with Message.match

// Match and unwrap message
Message.match(message)
    .isSetSize(new MatchAction1<Integer>() {
        @Override
        public void call(Integer value) {
            Log.d("Matched SetSize: " + value);
        }
    })
    .isReset(new MatchAction1() {
        @Override
        public void call() {
            Log.d("Matched Reset");
        }
    })
    .orElse(new MatchAction0() {
        @Override
        public void call() {
            Log.d("Matched wildcard");
        }
    });

Tagged unions are particularly useful for declaring the actions that an actor or thread can perform. To schedule an action with the actor, one need only create an instance of the actor's input type (the tagged union) and add the instance to the actor's message queue or stream. The actor then has access to the action type and parameters packed together in one neat object.

Download (Kotlin)

repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    compile 'com.github.wehjin.varnum:varnum:v1.1'
    kapt 'com.github.wehjin.varnum:varnum-compiler:v1.1'
}

Download (Java)

repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    compile 'com.github.wehjin.varnum:varnum:v1.1'
    apt 'com.github.wehjin.varnum:varnum-compiler:v1.1'
}

License

Copyright 2016 Jeffrey Yu.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

varnum's People

Contributors

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