When I define an enum class in Kotlin
enum class Answer {
YES,
NO
}
It has a valueOf(value: String) attached to it.
val doYouWantBeerOrCoffee = Answer.valueOf("YES") // Answer.YES
But where is this function actually defined? It is definitely not in the Enum.Kt and using Idea's Go to Implementation tool only takes me back to my Answer enum definition.
It's generated by the compiler. That's what "synthetic" means in
Enum classes in Kotlin have synthetic methods allowing to list the defined enum constants and to get an enum constant by its name.
If you decompile Answer.class you'll see it, but it isn't written as Kotlin (or Java) source code anywhere.
This method is a part of JDK, and defined in Enum.java class.
Which is the common base class of all Java language enumeration types.
Kotlin uses the same class for enums
Related
Kotlin any is a class, with the methods from Java Object
open class Any() {
open operator fun equals(other: kotlin.Any?): kotlin.Boolean { /* compiled code */ }
open fun hashCode(): kotlin.Int { /* compiled code */ }
open fun toString(): kotlin.String { /* compiled code */ }
}
Why is this?
I would have predicted that, as primitives are considered Any, but aren't actually Object, it would have better to have Any an interface that they pretended to implement rather than an class they pretended to inherit.
If Any were an interface, but every single class and interface implicitly inherits from it, then it would have to have default implementations of equals and hashcode, which wouldn't make conceptual sense for an interface.
Also, since every interface would inherit from Any, there would be a conflict when a class inherits from multiple interfaces. Which super-type implementation of equals and hashcode would be used? Multiple inheritance of interfaces is a very common situation, so you would be dealing with the complexity of conflicting interface function signatures very frequently rather than once in a blue moon.
Object does not exist in Kotlin. That is a Java concept. The fact that after compilation, the byte code is compatible with Java types such as Object, is irrelevant from a Kotlin language perspective.
Nat Pryce wrote a really short, very readable article explaining the Kotlin type system.
http://www.natpryce.com/articles/000818.html
In it you'll notice he says:
Any is the equivalent of Java’s Object class.
But they are not the same thing. The Java and Kotlin type systems are entirely separate things.
Any is not an interface because it serves nearly the same purpose as Java's Object. In Kotlin, Any? is the type that includes all types which can be represented in the type system. And Any is the type that includes all types except the null value. So really, Any? is Kotlin's Object. It is a type for the same reason that Object is a type.
As others have mentioned, Kotlin doesn't have primitives in its type system, so there is no need for them to inherit or pretend to inherit any interface. They don't exist.
Data classes print out just fine in MPP projects. When I toString() the KClass object for my class, I get:
class com.example.MySimpleClass (Kotlin reflection is not available)
How Can I do what data class does and have a nice clean name without reflection?
I don't have it set up myself to test, so answer is based purely on documentation:
KClass.simpleName is available in Common code; qualifiedName isn't, but since 1.3 it is on every platform, so you could define an expect fun in your multiplatform part and make all actual implementations access qualifiedName.
I`m new at kotlin and want to build a multiplatform application.
For the common part I want to use data classes that contains platform specific functions.
Is it possible to use the kotlin data class in a platform specific declaration?
something like
expect data class Foo(val bar: String)
best regards
From Kotlin's docs on Platform Specific Declarations:
Expected declarations never contain any implementation code.
Since data classes generate implementations they can't be used in expect declarations. The actual implementation can be a data class since it does not change the semantics of the declared class.
expect class Some {}
actual data class Some(val test: UUID)
I'm autogenerating code with KotlinPoet and Auto Service. I didn't find any way to know if an annotated class has "internal" modifier so I can create another class with same modifier. For example:
#MyAnnotation
internal class Car
So I thought using Kotlin Reflection I would be able to get this information but no luck.
With the annotator processor I'm able to get the KClass but the visibility said "public":
Any clues on how to do it?
Kotlin reflection is not applicable during annotation processing. Kotlin reflection is for inspecting your code at runtime.
However there is a way to parse the metadata out of Kotlin class files, it's called kotlinx-metadata-jvm.
To use this in your annotation processor, you'll have to obtain the AnnotationMirror of the kotlin.Metadata annotation. From that mirror, obtain the annotation values and use them to construct the KotlinClassHeader as you can see in the examples for kotlinx-metadata-jvm. Once you are there you can use kotlinx-metadata-jvm to extract the flags for your class.
Kotlin's KClass has the visibility property, which can be KVisibility.INTERNAL.
Since internal is a visibility modifier, you should look for something related in the KClass API. The following will help you:
/**
* Visibility of this class, or `null` if its visibility cannot be represented in Kotlin.
*/
#SinceKotlin("1.1")
public val visibility: KVisibility?
Used like this: Car::class.visibility //INTERNAL
I know that ABAP Objects are kinda old but as far as my knowledge goes you still have to use at least two "sections" to create a complete class.
ABAP:
CLASS CL_MYCLASS DEFINITION.
PUBLIC SECTION.
...
PROTECTED SECTION.
...
PRIVATE SECTION.
...
ENDCLASS.
CLASS CL_MYCLASS IMPLEMENTATION.
...
ENDCLASS.
Java:
public class MyClass {
<visibility> <definition> {
<implementation>
}
}
Wouldn't it make development easier/faster by having a combination of both like most modern languages have?
What are the reasons for this separation?
Easier/faster for the human (maybe), but costly for the compiler: It has to sift through the entire code to determine the structure of the class and its members, whereas in the current form, it only needs to compile the definition to determine whether a reference is valid. ABAP is not the only language that separates definition from implementation: Pascal did so for units, and Object Pascal for classes. One might argue that C++ allows for same construct without specifying an implementation section when you're not using inline member function declarations.
Maybe another reason:
Most (?) classes are not defined with manual written code, but via SE24. There you define the interface in one dynpro and write the code in another one.
Internally the interfaces are stored in one source, the code is stored in another source. So it is reasonable to separate the interface and the implementation.