Skip to content

Commit

Permalink
add class and member references
Browse files Browse the repository at this point in the history
  • Loading branch information
angryziber committed Nov 26, 2024
1 parent 254fe7e commit a85db20
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions docs/Kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@
* No magic needed with too many annotations
* Frontend often uses TypeScript, which shares more concepts with Kotlin than Java

| **Concept** | **Java** | **Kotlin** | **TypeScript** |
|------------------------------|---------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------|------------------------------------------------------------------------|
| **Variable Declaration** | `int age = 25;` | `val age: Int = 25` | `let age: number = 25;` |
| **Immutable Variable** | `final int age = 25;` | `val age: Int = 25` | `const age: number = 25;` |
| **Type inference** | `var age = 25;` (only for local variables) | `val age = 25` (anywhere, also for return types) | `let age = 25;` (anywhere, also for return types) |
| **Nullable declaration** | N/A (nulls allowed by default) | `var name: String? = null` | `name?: string` |
| **Function Declaration** | `int add(int a, int b) { return a + b; }` (only inside of classes) | `fun add(a: Int, b: Int) = a + b` | `function add(a: number, b: number) { return a + b }` |
| **Class Declaration** | `class Person {}` | `class Person` | `class Person {}` |
| **Constructor Parameters** | `public class Service { Dependency dependency; public Service(Dependency dependency) { this.dependency = dependency; } }` | `class Service(val dependency: Dependency)` | `class Service { constructor(public dependency: Dependency) {} }` |
| **Concept** | **Java** | **Kotlin** | **TypeScript** |
|------------------------------|---------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|--------------------------------------------------------------------------|
| **Variable Declaration** | `int age = 25;` | `val age: Int = 25` | `let age: number = 25;` |
| **Immutable Variable** | `final int age = 25;` | `val age: Int = 25` | `const age: number = 25;` |
| **Type inference** | `var age = 25;` (only for local variables) | `val age = 25` (anywhere, also for return types) | `let age = 25;` (anywhere, also for return types) |
| **Nullable declaration** | N/A (nulls allowed by default) | `var name: String? = null` | `name?: string` |
| **Function Declaration** | `int add(int a, int b) { return a + b; }` (only inside of classes) | `fun add(a: Int, b: Int) = a + b` | `function add(a: number, b: number) { return a + b }` |
| **Class Declaration** | `class Person {}` | `class Person` | `class Person {}` |
| **Data Class** | `record Person(String name, int age) {}` (no copy constructors) | `data class Person(val name: String, val age: Int)` with copy() methods | `{name: string, age: number}`, can be copied with `{...object, age: 21}` |
| **Inheritance** | `class Student extends Person {}` | `class Student: Person()` | `class Student extends Person {}` |
| **Interface Implementation** | `class Dog implements Animal {}` | `class Dog: Animal` | `class Dog implements Animal {}` |
| **Object Creation** | `Person person = new Person("John", 25);` | `val person = Person("John", 25)` | `let person = new Person("John", 25)` |
| **String Interpolation** | `String message = "Hello " + name;` | `val message = "Hello $name"` | ``let message = `Hello ${name}`` |
| **Default Parameters** | N/A | `fun greet(name: String = "Guest")` | `function greet(name: string = "Guest")` |
| **Extension functions** | N/A, usually static utility methods with parameters | `fun LocalDate.today() = ...` | `interface Date { function today() {...} }` |
| **Lambda Expressions** | `Runnable r = () -> System.out.println("Hi");` | `val greet = { println("Hi") }` | `const greet = () => { console.log('Hi') }` |
| **Access Modifiers** | `public`, `protected`, `private`, package-private by default | `public` (default), `protected`, `private`, `internal` | `public` (default), `protected`, `private` |
| **Generics** | `List<String> list = new ArrayList<>();` | `val list = ArrayList<String>()` | `let list: Array<string> = []` |
| **List creation** | `List.of(1, 2, 3);` | `listOf(1, 2, 3)` | `[1, 2, 3]` |
| **Map creation** | `Map.of("key1", 1, "key2", 2);` (null values not allowed) | `mapOf("key1" to 1, "key2" to 2)` | `{key1: 1, key2: 2}` |
| **Value transformation** | `list.stream().map(i -> i * 2).collect(toList());` | `list.map { it * 2 }` | `list.map(i => i * 2)` |
| **Semicolons** | Required | Optional | Optional |
| **Readable test names** | `@Test void myCoolMethodDoesThisAndThat() {}` | `@Test fun ``my cool method does this and that``() {}` | `test('my cool method does this and that')` |
| **Asynchronous Code** | `CompletableFuture.supplyAsync(() -> ...)` (no syntactic support) | `suspend` functions built-in | `async function fetchData() { ... }` async/await built-in |
| **Constructor Parameters** | `public class Service { Dependency dependency; public Service(Dependency dependency) { this.dependency = dependency; } }` | `class Service(val dependency: Dependency)` | `class Service { constructor(public dependency: Dependency) {} }` |
| **Class reference** | `Person.class` | `Person::class` | `Person` |
| **Member reference** | `Person.class.getMethod("myMethod", ...)` | `Person::myMethod` (type-safe reflection) | `Person.prototype.myMethod` |
| **Inheritance** | `class Student extends Person {}` | `class Student: Person()` | `class Student extends Person {}` |
| **Interface Implementation** | `class Dog implements Animal {}` | `class Dog: Animal` | `class Dog implements Animal {}` |
| **Object Creation** | `Person person = new Person("John", 25);` | `val person = Person("John", 25)` | `let person = new Person("John", 25)` |
| **String Interpolation** | `String message = "Hello " + name;` | `val message = "Hello $name"` | ``let message = `Hello ${name}`` |
| **Default Parameters** | N/A | `fun greet(name: String = "Guest")` | `function greet(name: string = "Guest")` |
| **Extension functions** | N/A, usually static utility methods with parameters | `fun LocalDate.today() = ...` | `interface Date { function today() {...} }` |
| **Lambda Expressions** | `Runnable r = () -> System.out.println("Hi");` | `val greet = { println("Hi") }` | `const greet = () => { console.log('Hi') }` |
| **Access Modifiers** | `public`, `protected`, `private`, package-private by default | `public` (default), `protected`, `private`, `internal` | `public` (default), `protected`, `private` |
| **Generics** | `List<String> list = new ArrayList<>();` | `val list = ArrayList<String>()` | `let list: Array<string> = []` |
| **List creation** | `List.of(1, 2, 3);` | `listOf(1, 2, 3)` | `[1, 2, 3]` |
| **Map creation** | `Map.of("key1", 1, "key2", 2);` (null values not allowed) | `mapOf("key1" to 1, "key2" to 2)` | `{key1: 1, key2: 2}` |
| **Value transformation** | `list.stream().map(i -> i * 2).collect(toList());` | `list.map { it * 2 }` | `list.map(i => i * 2)` |
| **Semicolons** | Required | Optional | Optional |
| **Readable test names** | `@Test void myCoolMethodDoesThisAndThat() {}` | `@Test fun ``my cool method does this and that``() {}` | `test('my cool method does this and that')` |
| **Asynchronous Code** | `CompletableFuture.supplyAsync(() -> ...)` (no syntactic support) | `suspend` functions built-in | `async function fetchData() { ... }` async/await built-in |

See [TSGenerator](../json/src/TSGenerator.kt) in klite-json for generating TypeScript interfaces from Kotlin data classes.

0 comments on commit a85db20

Please sign in to comment.