Jota

所属分类:数学计算
开发工具:kotlin
文件大小:0KB
下载次数:0
上传日期:2019-09-02 03:32:21
上 传 者sh-1993
说明:  Kotlin编程中代数数据类型的声明式模式匹配解
(Declarative pattern matching solution to algebraic data types in Kotlin programming)

文件列表:
LICENSE (11357, 2019-09-01)
build.gradle (812, 2019-09-01)
gradle.properties (26, 2019-09-01)
gradle/ (0, 2019-09-01)
gradle/wrapper/ (0, 2019-09-01)
gradle/wrapper/gradle-wrapper.jar (55190, 2019-09-01)
gradle/wrapper/gradle-wrapper.properties (202, 2019-09-01)
gradlew (5305, 2019-09-01)
gradlew.bat (2269, 2019-09-01)
jota-compiler/ (0, 2019-09-01)
jota-compiler/build.gradle (1462, 2019-09-01)
jota-compiler/gradle/ (0, 2019-09-01)
jota-compiler/gradle/wrapper/ (0, 2019-09-01)
jota-compiler/gradle/wrapper/gradle-wrapper.jar (55190, 2019-09-01)
jota-compiler/gradle/wrapper/gradle-wrapper.properties (202, 2019-09-01)
jota-compiler/gradlew (5305, 2019-09-01)
jota-compiler/gradlew.bat (2269, 2019-09-01)
jota-compiler/settings.gradle (36, 2019-09-01)
jota-compiler/src/ (0, 2019-09-01)
jota-compiler/src/main/ (0, 2019-09-01)
jota-compiler/src/main/kotlin/ (0, 2019-09-01)
jota-compiler/src/main/kotlin/JotaCommandLineProcessor.kt (433, 2019-09-01)
jota-compiler/src/main/kotlin/JotaComponentRegistrar.kt (446, 2019-09-01)
jota-compiler/src/main/kotlin/matcher/ (0, 2019-09-01)
jota-compiler/src/main/kotlin/matcher/DeclarativePatternMatchingPlugin.kt (304, 2019-09-01)
jota-compiler/src/main/kotlin/matcher/adt/ (0, 2019-09-01)
jota-compiler/src/main/kotlin/matcher/adt/AdtMatcher.kt (478, 2019-09-01)
jota-compiler/src/main/kotlin/matcher/patternmatching/ (0, 2019-09-01)
jota-compiler/src/main/kotlin/matcher/patternmatching/FunctionInfo.kt (102, 2019-09-01)
jota-compiler/src/main/kotlin/matcher/patternmatching/PatternMatcher.kt (2842, 2019-09-01)
jota-compiler/src/main/kotlin/matcher/patternmatching/PhaseFunctionOccurrence.kt (175, 2019-09-01)
jota-compiler/src/main/kotlin/meta/ (0, 2019-09-01)
jota-compiler/src/main/kotlin/meta/MetaCliProcessor.kt (407, 2019-09-01)
jota-compiler/src/main/kotlin/meta/MetaPlugin.kt (555, 2019-09-01)
jota-compiler/src/main/kotlin/meta/autofold/ (0, 2019-09-01)
jota-compiler/src/main/kotlin/meta/autofold/AutoFoldPlugin.kt (4345, 2019-09-01)
jota-compiler/src/main/kotlin/meta/comprehensions/ (0, 2019-09-01)
jota-compiler/src/main/kotlin/meta/comprehensions/ComprehensionsPlugin.kt (2247, 2019-09-01)
... ...

# Jota #### A way to uncouple algebraic data type checks This project is still in beta and is being tested and improved. ```kotlin sealed class Action { object Success object NetworkError object ParsingError } fun run(action: Action) = handleAction(action) private fun handleAction(@When action: Action.Success) = println("Success") private fun handleAction(@When action: Action.NetworkError) = println("NetworkError") private fun handleAction(@When action: Action.ParsingError) = println("ParsingError") run(Action.Success) // prints Success run(Action.NetworkError) // prints NetworkError run(Action.ParsingError) // prints ParsingError ``` Simply by using `@When` provided annotation in a type function parameter you can create a solution that implicitly infers the type and you don't need to worry about creating more code only to check them. ## Motivation In Kotlin development is common to explicitly check types but sometimes a simple solution with that can transform the code in a really verbose one, let's create a simple example using `when` with the code above: ```kotlin ... fun run(action: Action) { when(action) { is Action.Success -> handleAction(action) is Action.NetworkError -> handleAction(action) is Action.ParsingError -> handleAction(action) ... else -> {} } } ``` Here we have the same behavior that we've created with Jota but with explicity type checking, why should we check every single type only to do the same thing? Well, we can say in this simple example we're actually writing a verbose code that check different types and create its handling, but this isn't just a simple useless code, this solution breaks a very important best practice principle. ## The problem As said before this kind of explicitly type checking solution breaks a very important best practice, the `Open Closed Principle` that tell us that our code `should be opened for extension but closed for modification` and that means that we shouldn't have to modify our `run` function with something like `is Action.Type -> {}` every time we create more types but have a solution that we just care about extension like create new functions. ## Jota solution Jota provides a `@When` annotation that implicitly infers types for you then you can just worry about create extensible solutions: ```kotlin fun run(action: Action) = handleAction(action) private fun handleAction(@When action: Action.Success) = println("Success") private fun handleAction(@When action: Action.NetworkError) = println("NetworkError") private fun handleAction(@When action: Action.ParsingError) = println("ParsingError") run(Action.Success) // prints Success run(Action.NetworkError) // prints NetworkError run(Action.ParsingError) // prints ParsingError ``` then now if you have more types, you just need to worry about create new functions with its handling and not worry about modify any code only to add one more type checking, respecting the open closed principle: ```kotlin ... private fun handleAction(@When action: Action.JsonFieldError) = println("JsonFieldError") run(Action.JsonFieldError) // prints JsonFieldError ``` ## Internals Jota uses a mocked abstraction of [Arrow Meta Prototype](https://github.com/47deg/arrow-meta-prototype) to resolve compiler plugin internals and will be correctly imported when it's released. ## Contributions Contributions are welcome, bugs and features should be reported by issues in this repository. ## Import In your buildScript dependency config add: ```groovy dependencies { classpath "com.bloder:jota-plugin:0.0.1" } ``` And in your build gradle dependency ```groovy apply plugin: "jota-plugin" dependencies { implementation "com.bloder:jota:0.0.1" } ```

近期下载者

相关文件


收藏者