Reputation: 15
I am trying to build an ObjectBox query in Kotlin. Here is my code:
fun getDesignByGameName(gameName: String): ScoreSheetDesignEntity? {
val query = scoreSheetDesignBox.query {
ScoreSheetDesignEntity_.gameName equal gameName // <<<<< error occurs on this line
}
val entity = query.findFirst()
return entity
}
I am getting a syntax error on the line indicated above:
'infix' modifier is required on 'FirNamedFunctionSymbol io/ objectbox/ Property. equal'.
I have the following in my Gradle build file (where the version is 4.0.3
):
implementation "io.objectbox:objectbox-java:$objectbox_version"
implementation "io.objectbox:objectbox-kotlin:$objectbox_version"
This is the only query I have written so far that is getting a syntax error. I’m following the example in the ObjectBox doc. Any idea what I’m doing wrong, or what I am missing?
Upvotes: 0
Views: 34
Reputation: 1327
Use round parentheses:
val query = scoreSheetDesignBox.query(
ScoreSheetDesignEntity_.gameName equal gameName
).build()
Using curly braces uses the legacy query API which does not support infixes in Kotlin.
Upvotes: 0
Reputation: 18577
(A partial answer…)
As you probably know, in Kotlin methods are normally called with the syntax object.method(params…)
. There is also an infix syntax — object method param
— but because it can be ambiguous and confusing, it's pretty rare, mostly used only for a few things like numerical/boolean/bitwise operators and some DSLs. To reduce confusion, the infix syntax can only be used on methods specifically intended for it, which is indicated by an infix
modifier on the method definition. (Such methods must also take exactly one parameter, with no default value.)
So the error message you're getting indicates that the ObjectBox equal()
method you're using was defined without that modifier. That means you can't use infix syntax with it, and must use the normal method-call notation with dot and parens.
I don't know ObjectBox, and its docs aren't very clear on the different versions, so I don't understand exactly what's going on here. As you say, in the ObjectBox queries page, some of the Kotlin examples use infix notation — but others don't, which is odd.
Either way, my guess is that it's getting that method from the Java libraries (which of course won't have that modifier) instead of the Kotlin ones. So the problem is probably with your Gradle config.
The ObjectBox Getting Started page doesn't mention either of the dependencies you show in your Gradle file. However, the Advanced Setup page does — and in the same order. So there's nothing obviously wrong with what you're doing. Are you also adding and applying the io.objectbox
plugin, and all the other bits of Gradle config they show there?
If that doesn't lead to a solution, could you try using the simplified Gradle set-up shown on the Getting Started page instead?
Upvotes: 0