ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - AhmedMourad0/no-copy: A Kotlin compiler plugin that removes the `copy` method of data classes.
A Kotlin compiler plugin that removes the `copy` method of data classes. - AhmedMourad0/no-copy
Visit Site

GitHub - AhmedMourad0/no-copy: A Kotlin compiler plugin that removes the `copy` method of data classes.

GitHub - AhmedMourad0/no-copy: A Kotlin compiler plugin that removes the `copy` method of data classes.

NoCopy Compiler Plugin CI Maven Central

Usage

Include the gradle plugin in your project and apply @NoCopy to your data class.

@NoCopy

@NoCopy prevents the kotlin compiler from generating the copy method:

@NoCopy
data class User(val name: String, val phoneNumber: String)
User("Ahmed", "+201234567890").copy(phoneNumber = "Happy birthday!") // Unresolved reference: copy

Why? I hear you ask.

The copy method of Kotlin data classes is a known language design problem, normally, you can't remove it, you can't override it, and you can't document it.

Why would you want to do that? Well, there are a couple of reasons:

  • copy is a guaranteed source of binary incompatibility as you add new properties to the type when all you wanted was value semantics.
  • If you want value-based classes, copy will break your constructor invariants.
  • Private constructors are basically meaningless as long as copy exists.

Consider something like this:

data class User private constructor(val name: String, val phoneNumber: String) {
    companion object {
        fun of(name: String, phoneNumber: String): Either<UserException, User> {
            return if (bad) {
                exception.left() //You can throw an exception here if you like instead.
            } else {
                User(name, phoneNumber).right()
            }
        }
    }
}

It would look like all instances of User must be valid and can't be bad, right?

Wrong:

User.of("Ahmed", "+201234567890").copy(phoneNumber = "Gotcha")

copy can bypass all the validations of your data class, it breaks your domain rules!

For more detailed explanation, check out this article.

Installation

Using plugins DSL

  • In your module-level build.gradle:
plugins {
  id "dev.ahmedmourad.nocopy.nocopy-gradle-plugin" version "1.5.0"
}

Using legacy plugin application

  • In your project-level build.gradle:
buildscript {
    repositories {
        mavenCentral()
        // Or
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "dev.ahmedmourad.nocopy:nocopy-gradle-plugin:1.5.0"
    }  
}
  • In your module-level build.gradle:
// For each module that needs to use the annotations
apply plugin: 'dev.ahmedmourad.nocopy.nocopy-gradle-plugin'

IDE Support

  • Install the IDEA plugin File -> Settings -> plugins -> Marketplace -> Kotlin NoCopy

  • Disable the default inspection File -> Settings -> Editor -> Inspections -> Kotlin -> Probably bugs -> Private data class constructor is.... Currently, you have to do this manually due to a bug with the Kotlin plugin, upvote.

Caveats

  • Kotlin compiler plugins are not a stable API. Compiled outputs from this plugin should be stable, but usage in newer versions of kotlinc are not guaranteed to be stable.

Versions

Kotlin Version NoCopy Version
1.9.20 1.5.0
1.5.0 1.4.0
1.4.32 1.3.0
1.4.20 1.2.0
1.4.0 1.1.0
1.3.72 1.0.0

License

Copyright (C) 2020 Ahmed Mourad

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.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory