is there some kind of trick to understand this low...
# announcements
t
is there some kind of trick to understand this low level kotlin syntax ? I dont understand what is Key, T and * in here
Copy code
fun <Key:Comparable<Key>, T: IdTable<Key>> T.insertIgnoreAndGetId(body: T.(UpdateBuilder<*>)->Unit) = InsertStatement<EntityID<Key>>(this, isIgnore = true).run {
    body(this)
    execute(TransactionManager.current())
    getOrNull(id)
}
j
Key and T are generic parameters with restrictions that Key must implement Comparable and T must be of type IdTable.. The next part makes this function an extension on T. The <*> just means it will accept any UpdateBuilder regardless of parameter type, or in this case any function reference on T that takes an UpdateBuilder parameter and returns Unit.
z
Not sure what you mean by "low-level". Those things are generic type parameters, which is a core kotlin feature that is used by kotlin code all the time: https://kotlinlang.org/docs/tutorials/kotlin-for-py/generics.html
e
To pull it out piece by piece… •
<Key:Comparable<Key>, T: IdTable<Key>>
are the generic type parameters. They assert that
Key
implements comparable against itself, and
T
implements
IdTable<Key>
T.insertIgnoreAndGetId
defines the method as an extension function on
T
, the table •
body: T.(UpdateBuilder<*>)->Unit
means the parameter,
body
accepts a function returning
Unit
which is either an extension or member of
T
that gets passed a parameter of type
UpdateBuilder<*>
, where `UpdateBuilder`’s generic type doesn’t matter (it’s a wildcard) •
= InsertStatement<EntityID<Key>>(this, isIgnore = true).run {
means this is a single-expression function, returning the result of
InsertStatement.run()
. It constructs an insert statement and runs the “run” block, returning the “run” block’s value •
body(this)
executes the function passed as a parameter, using
this
(the insert statement) as the parameter and receiver •
execute(TransactionManager.current())
is calling
InsertStatement.execute()
on the current transaction manager to perform the SQL query •
getOrNull(id)
is executing
InsertStatement.getOrNull()
to retrieve the value of the
id
column from the result. As this is the last statement in the “run” block, either the value of the ID column is returned, or Null on an error Hope that helped! And I highly recommend checking out the resources that other users posted above.
1
👍 2
👏 2