Skip to content

Generate constructor for schema interface #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nikitinas opened this issue May 21, 2022 · 2 comments
Open

Generate constructor for schema interface #113

nikitinas opened this issue May 21, 2022 · 2 comments
Assignees
Labels
research This requires a deeper dive to gather a better understanding
Milestone

Comments

@nikitinas
Copy link
Contributor

nikitinas commented May 21, 2022

Support GenerateConstructor annotation on companion object of schema interface to generate default implementation of that interface and append overload for DataFrame:

@DataSchema
interface Record {
    val a: Int
    val b: Int

    @GenerateConstructor
    companion object
}

// region Generated Code

operator fun Record.Companion.invoke(a: Int, b: Int): Record =
    object: Record {
        override val a = a
        override val b = b
    }

fun DataFrame<Record>.append(vararg rows: Record) = concat(rows.asIterable().toDataFrame())

// endregion

// usage:

listOf(Record(1,2), Record(3,4))
  .toDataFrame()
  .append(Record(5,6))
  .add("sum") { a + b }
@nikitinas nikitinas changed the title Generate constructor for DataSchema Generate constructor for schema interface May 21, 2022
@nikitinas
Copy link
Contributor Author

nikitinas commented Jun 14, 2022

Better design.

DataFrame API:

interface DataRowSchema

inline fun <reified T:DataRowSchema> dataFrameOf(vararg rows: T): DataFrame<T> = rows.asIterable().toDataFrame()

inline fun <reified T:DataRowSchema> DataFrame<T>.append(vararg rows: T): DataFrame<T> = concat(dataFrameOf(*rows))

User code:

@DataSchema
interface Record: DataRowSchema {
    val a: Int
    val b: Int

    companion object
}

// region Generated code

operator fun Record.Companion.invoke(a: Int, b: Int): Record =
    object: Record {
        override val a = a
        override val b = b
    }

// endregion

// usage:

dataFrameOf(Record(1,2), Record(3,4))
  .append(Record(5,6))
  .add("sum") { a + b }

@koperagen
Copy link
Collaborator

koperagen commented Jun 14, 2022

Can we somehow get rid of DataRowSchema?
In FIR, it's possible to add supertypes to annotated class, so at least it can disappear from user code.

And here is another example to be aware of

// User code:
@DataSchema
interface AnotherRecord: DataRowSchema {
    val a: Int
    val b: Int

    companion object
}

@DataSchema
interface Record: DataRowSchema {
    val a: List<AnotherRecord> // same as DataFrame<AnotherRecord>
    val b: AnotherRecord // same as DataRow<AnotherRecord>

    companion object
}

// usage:
dataFrameOf(Record(listOf(AnotherRecord(5,6)), AnotherRecord(5,6)))

@zaleslaw zaleslaw added the research This requires a deeper dive to gather a better understanding label Apr 25, 2023
@zaleslaw zaleslaw added this to the 0.11.0 milestone Apr 25, 2023
@zaleslaw zaleslaw modified the milestones: 0.11.0, Backlog Jun 22, 2023
koperagen added a commit that referenced this issue Apr 2, 2024
Idea was proposed here #113
Better to have it as a part of the compiler plugin.
Plus, using a class instead of an interface already provides the ability to create "rows"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
research This requires a deeper dive to gather a better understanding
Projects
None yet
Development

No branches or pull requests

3 participants