Giter VIP home page Giter VIP logo

result-type's Introduction

Rust's result type and question mark like operator for java

This repository contains a simple implementation of Rust's result type and question mark like operator for java. Since ? is already symbol for defining types in java and no other way of calling it is syntactically valid we will use a method tryGet() to achieve the same effect. This plugin works by replacing the bytecode of result.tryGet() with the bytecode equivalent to below code

if (!result instanceof Ok) {
    return Err(ErrorType.from(result.error())
}

Warning: This plugin is a project for fun and not tested in production and may have bugs. Please use it at your own risk.

How to use

The module test-module contains sample code that shows how to use the result type and the tryGet() method. The module result contains the implementation of the result type and the tryGet() method.

  • Clone this repository and run mvn clean install. This step is required because these artifacts are not available in any maven repository.
  • Add the following dependency to your pom.xml
<dependency>
    <groupId>org.example</groupId>
    <artifactId>result-type-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
  • Add the result-type-plugin to your pom.xml
<build>
    <plugins>
        <plugin>
            <groupId>org.example</groupId>
            <artifactId>result-type-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>return-result</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>process</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • Start using the result type and the tryGet() method. ensure that any function that uses the tryGet() method returns a Result type.
public Result<String, StdError> method1() {
    Result<String, StdError> res = getRes();
    System.out.println(res.errorContext("Failed to get hello").tryGet());
    return res;
}

Caveats

  • Any method that uses tryGet() must return a Result type.
  • The Error part of the Returned Result type must not be a generic type like T, U or an unbounded type like ?.
  • You can define your own error type by implementing the IError interface. but ensure that the type implements a static method from that takes IError as input and returns the error type. look at the StdError class for an example.
    • The static method is chosen over constructor because
      1. You cannot call constructors in enums, interfaces or abstract classes
      2. This is much closer to Rust's api.
      3. Constructors take 4 instructions (new, dup, current error, ) and 2 new entries in the stack
  • Sometimes you may get error that StackMapFrames are inconsistent. This is because I don't fully understand it and somehow made it work. Incase you face this issue the workaround is to comment the aa.visitFrame(F_NEW, locals.size(), locals.toArray(), stack.size(), stack.toArray()); line in ResultTypeMethodVisitor class and run your java application with -noverify flag
    • This is not a good solution and if someone knows how to fix this please raise a PR.

result-type's People

Contributors

saisandeep-flipkart avatar

Stargazers

Tim Kersey avatar

Watchers

 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.