NoCopy Compiler Plugin alternatives and similar libraries
Based on the "Multiplatform" category.
Alternatively, view NoCopy Compiler Plugin alternatives based on common mentions on social networks and blogs.
-
🍲 Foodium (Kotlin Multiplatform Mobile)
📱Sample application built to demonstrate the use of Kotlin Multiplatform Mobile for developing Android and iOS applications using Jetpack Compose 🚀. -
Kotlin Spotify Web API
Spotify Web API wrapper for Kotlin, Java, JS, and Native - Targets JVM, Android, JS (browser), Native (Desktop), and Apple tvOS/iOS. Includes a Spotify Web Playback SDK wrapper for Kotlin/JS, and a spotify-auth wrapper for Kotlin/Android. -
Generative AI SDK for KMP
✨Generative AI SDK for Kotlin Multiplatform (Supports: JVM, Android, iOS, Desktop, Web JS, Wasm) -
krontab
Library for using Crontab-like syntax in scheduling of some Kotlin Coroutines tasks to do from time to time -
NonEmptyCollections
A type-safe implementation for collections that cannot be empty. Life is too short for emptiness-checks!
InfluxDB - Purpose built for real-time analytics at any scale.
Do you think we are missing an alternative of NoCopy Compiler Plugin or a related project?
README
NoCopy Compiler Plugin
A Kotlin compiler plugin that removes the copy
method from data classes
and enables using them as value-based classes.
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 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 explaination, check out this article.
Installation
- In your project-level
build.gradle
:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "dev.ahmedmourad.nocopy:nocopy-gradle-plugin:1.1.0"
}
}
- In your module-level
build.gradle
:
// For each module that needs to use the annotations
apply plugin: 'dev.ahmedmourad.nocopy.nocopy-gradle-plugin'
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
Currently, you cannot have a method named
copy
with the same signature (return type included) in your@NoCopy
annotated data class or you will get IDE and compiler errors. (Attempting this, however, can be considered a bad practice ascopy
has a very defined behaviour inKotlin
, replacing it with your own custom implementation can be misleading)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.3.72 | 1.0.0 |
1.4.0 | 1.1.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.
*Note that all licence references and agreements mentioned in the NoCopy Compiler Plugin README section above
are relevant to that project's source code only.