pavel
11/15/2018, 12:08 AMval foo = gitlabEvent.webhookEvent.objectAttributes != null && gitlabEvent.webhookEvent.objectAttributes.action == Action.OPEN
gitlabEvent
and webhookEvent
are non-nullable. objectAttributes
is nullable. The compiler is telling me Smart cast to 'ObjectAttributes' is impossible, because 'gitlabEvent.webhookEvent.objectAttributes' is a complex expression
. I thought smart casting was smarter than that 😞dpk
11/15/2018, 12:13 AMwebhookEvent
provided by a separate module or some dependency? if so, that’d explain the errorpavel
11/15/2018, 12:15 AMpavel
11/15/2018, 12:16 AMwebhookEvent
is a data Class. So are objectAttributes
pavel
11/15/2018, 12:17 AMinterface GitlabEvent {
val webhookEvent: WebhookEvent
val filter: String
fun buildSlackMessage(): SlackMessage
}
pavel
11/15/2018, 12:17 AM@JsonIgnoreProperties(ignoreUnknown = true)
data class WebhookEvent(
@JsonProperty("object_kind") val objectKind: WebhookEventType,
val project: Project,
val user: User,
@JsonProperty("object_attributes") val objectAttributes: ObjectAttributes? = null,
@JsonProperty("merge_request") val mergeRequest: MergeRequest? = null,
val commit: Commit? = null,
val issue: Issue? = null,
val snippet: Snippet? = null
)
pavel
11/15/2018, 12:17 AM@JsonIgnoreProperties(ignoreUnknown = true)
data class ObjectAttributes(
val id: Int,
val action: Action = Action.UNKNOWN,
val title: String = "",
val url: String,
val iid: Int,
val note: String = "",
@JsonProperty("noteable_type") val noteableType: NoteableType = NoteableType.UNKNOWN
)
dpk
11/15/2018, 12:24 AMval x = Fred(Bar(Foo("xyz")))
data class Foo(val label: String)
data class Bar(val foo: Foo? = null)
data class Fred(val bar: Bar)
fun doTheFoo() { val y = x.bar.foo != null && x.bar.foo.label == "xyz" }
i do get exactly that error if i declare x
with var
, but it looks like that’s not the case here. stumped.pavel
11/15/2018, 12:25 AMpavel
11/15/2018, 12:39 AMval webhookEvent = gitlabEvent.webhookEvent
val foo = webhookEvent.objectAttributes != null && webhookEvent.objectAttributes.action == Action.OPEN
compilesdpk
11/15/2018, 12:46 AMval x: Fred = FredImpl(Bar(Foo("xyz")))
data class Foo(val label: String)
data class Bar(val foo: Foo? = null)
interface Fred { val bar: Bar }
data class FredImpl(override val bar: Bar) : Fred
fun doTheFoo() { val y = x.bar.foo != null && x.bar.foo.label == "xyz" }
demonstrates the problem. i ain’t smart enough to know the cause, but there ya go, i see the same thing now.pavel
11/15/2018, 12:46 AMpavel
11/15/2018, 12:49 AMkarelpeeters
11/15/2018, 12:49 AMpavel
11/15/2018, 12:50 AMval
. It can’t magically become a var
if overridenkarelpeeters
11/15/2018, 12:51 AMval myNonVar: String?
get() = if (Random().nextBoolean()) "test" else null
pavel
11/15/2018, 12:52 AMpavel
11/15/2018, 12:54 AMpavel
11/15/2018, 12:54 AMkarelpeeters
11/15/2018, 12:58 AMpavel
11/15/2018, 1:01 AMstevecstian
11/15/2018, 1:23 AMval
further.aarjav
11/15/2018, 3:26 AM?.
still work? As in val foo = gitlabEvent.webhookEvent.objectAttributes?.action == Action.OPEN
pavel
11/15/2018, 3:28 AMgitlabEvent.webhookEvent.objectAttributes?.action
might return null
, and it’s perfectly valid to compare null
with other stuff