https://kotlinlang.org logo
#announcements
Title
# announcements
v

vlad.minaev

07/12/2019, 10:52 AM
In TypeScript there is a
Pick
type which lets you easily (and type-safe) define sub-shape:
Copy code
interface Todo {
    title: string;
    description: string;
    completed: boolean;
}

type TodoPreview = Pick<Todo, 'title' | 'completed'>; // you can only specify properties of Todo here

const todo: TodoPreview = {
    title: 'Clean room',
    completed: false,
};
I'm trying to find out whether it's possible to achieve something like this in Kotlin
k

karelpeeters

07/12/2019, 10:53 AM
Not really, but you can define interfaces and separate data classes of course.
t

tddmonkey

07/12/2019, 10:54 AM
a sealed class hierarchy might achieve what you want
v

vlad.minaev

07/12/2019, 10:59 AM
not the case, I want to use java classes as source
t

tddmonkey

07/12/2019, 11:03 AM
I’m not sure I follow then
v

vlad.minaev

07/12/2019, 11:04 AM
see the example in the chat
t

tddmonkey

07/12/2019, 11:25 AM
You could create your own builder for it then
But you could also mirror your types in a sealed class hierarchy to achieve it
Or write an annotation processor
v

vlad.minaev

07/12/2019, 11:28 AM
yep, I cannot find anything better then annotation processor
still not clear how to make compile time property check
a

Alowaniak

07/12/2019, 11:33 AM
I'm not sure if a Pick-like construct is very useful in a non-structural typing system though?
v

vlad.minaev

07/12/2019, 11:34 AM
I brought it just to demonstrate property referencing
5 Views