I wanted to think I understood Elm's custom types in Java

This is a note that Uncle Java thought about in Java to understand Elm's custom types. I'm not familiar with stag beetles.

Take Result as an example of a custom type:

Elm Result definition


type Result error value
    = Ok value
    | Err error

An example of Java representation of the above Result:

Result.java


interface Result {
    class Ok<V> implements Result {
        final V value;
        Ok(V value) { this.value = value; }
    }

    class Err<E> implements Result {
        final E error;
        Err(E error) { this.error = error; }
    }
}

--I skipped around the access modifier. --I'm not doing my best because the purpose is not to ** reproduce ** custom types in Java. ――I really did my best (I gave Result ʻE and V`), but I stopped because it was troublesome.

Example of actually using the Java version Result type:

Main.java


class Main {
    public static void main(String[] args) {
        show(validate("100"));     // -> Ok: 100
        show(validate("200hoge")); // -> Err:Please enter a number
    }

    /**
     *Verify that text is a string consisting only of numbers.
     * @param text The string to be validated.
     * @Integer if return text consists only of numbers.parseInt(text)The result of
     *Result set in the value field.Ok 。<br>
     *If the text contains non-numeric characters"Please enter a number"To
     *Result set in the error field.Err 。
     */
    static Result validate(String text) { //"Elm understood from the basics" P.91 validate functions are the source
        try {
            var n = Integer.parseInt(text);
            return new Result.Ok<>(n);
        } catch (NumberFormatException e) {
            return new Result.Err<>("Please enter a number");
        }
    }

    /**
     *Output the contents of result to standard output.
     * @param result The value to output.
     */
    static void show(Result result) {
        if (result instanceof Result.Ok) {
            var ok = (Result.Ok<Integer>) result;
            System.out.println("Ok: " + ok.value);
        } else if (result instanceof Result.Err) {
            var err = (Result.Err<String>) result;
            System.out.println("Err: " + err.error);
        } else {
            throw new RuntimeException("Mystery type is specified");
        }
    }
}

It's a code that doesn't look cool because it depends heavily on the ʻIntegerandString specified in the type parameter, and it branches at the ʻinsutanceof. I guess I didn't do my best. ~~ In such an atmosphere. ~~

The current perception is that Elm's custom types look like this in Java. Originally, you should learn algebraic data types properly, but for the time being, my understanding has advanced (I feel), so Yoshi! I am.

Recommended Posts

I wanted to think I understood Elm's custom types in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I just wanted to make a Reactive Property in Java
I want to send an email in Java.
I tried to implement deep learning in Java
rsync4j --I want to touch rsync in Java.
I tried to output multiplication table in Java
I tried to create Alexa skill in Java
I want to do something like "cls" in Java
I tried to implement Firebase push notification in Java
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried to make a login function in Java
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I tried to find out what changed in Java 9
I made roulette in Java.
[Beginner] I made a program to sell cakes in Java
I investigated Java primitive types
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I tried using Dapr in Java to facilitate microservice development
I tried to make a client of RESAS-API in Java
I tried metaprogramming in Java
I want to simplify the conditional if-else statement in Java
[Java] I want to perform distinct with the key in the object
I think it's okay to write joins etc. in the scope
I sent an email in Java
Multithreaded to fit in [Java] template
I created a PDF in Java.
I tried to interact with Java
Log output to file in Java
I wrote Goldbach's theorem in java
The story I wanted to unzip
I made an annotation in Java.
I tried to summarize Java learning (1)
How to name variables in Java
Try to implement Yubaba in Java
List of types added in Java 9
I tried to summarize Java 8 now
How to concatenate strings in java
I want to select multiple items with a custom layout in Dialog
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I wanted to implement a slide show in a fashionable way with slick.
I called the COTOHA API parser 100 times in Java to measure performance.
I tried to make a talk application in Java using AI "A3RT"
I want to ForEach an array with a Lambda expression in Java
[Java Spring MVC] I want to use DI in my own class
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
I tried using Elasticsearch API in Java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to use custom helpers in rails
Java reference to understand in the figure
Try to implement n-ary addition in Java
I tried to summarize Java lambda expressions
I just wanted to logrotate with log4j 2
I tried the new era in Java