https://kotlinlang.org logo
Title
e

elect

08/26/2019, 4:55 PM
Trying to have enums backed by an
IntBuffer
, I'm writing an extension function for an Enum, where its companion object extends a specific interface. But if I cast, the compiler says the cast can never succeed.. why?
fun <E> Enum<E>.bind() where E : Enum<E>, E : A = (Enum.Companion as B).buffers[ordinal].bind() // this cast can never succeed
s

Sam Schilling

08/26/2019, 5:01 PM
Also don’t you need to put the return type of the function before the where clause?
e

elect

08/26/2019, 5:01 PM
this fails
Enum.Companion as B
it's
Unit
j

jw

08/26/2019, 5:02 PM
Because that refers to the companion of the
Enum
class, not the companion of the receiver
s

Sam Schilling

08/26/2019, 5:02 PM
oh I see I misread that
e

elect

08/26/2019, 5:03 PM
is there a way ro retrieve the receiver companion?
r

Ruckus

08/26/2019, 5:08 PM
There is no way in the type system to specify "a class that has a companion object" (let alone of a specific type)
d

Dominaezzz

08/26/2019, 5:16 PM
A decent compromise is to replace
Enum<E>
with an interface that can return it's "companion object". Won't be type safe but .... it works 😅.
r

Ruckus

08/26/2019, 5:31 PM
Or change
bind
to accept a
B
e

elect

08/26/2019, 5:35 PM
@Dominaezzz problem is that then the buffers would be static per interface. I need to have them per enum. Because I'd like to have the possibility to have multiple enums (extending the same interface) backing each different objects
d

Dominaezzz

08/26/2019, 5:36 PM
From the interface you decide what object you want to return,
e

elect

08/26/2019, 5:40 PM
can you elaborate @Dominaezzz?
r

Ruckus

08/26/2019, 5:43 PM
That gist has several problems with generics
d

Dominaezzz

08/26/2019, 5:44 PM
GlBufE
can have a method that returns an instance of
GlBufEco
.
👍 1
e

elect

08/26/2019, 5:44 PM
yeah yeah, it's just a draft
r

Ruckus

08/26/2019, 5:49 PM
Sorry, I only meant I don't understand what each placeholder is supposed to do as the generics don't line up. But I'd go with @Dominaezzz's suggestion if it makes sense (you'd probably have to change the function to
fun <E> E.bind(...
instead of
Enum<E>.bind(...
)
Maybe something like this: https://pl.kotl.in/7LQEa_sRz (Assuming I didn't totally screw up the generics)
d

Dominaezzz

08/26/2019, 6:09 PM
Tiny upgrade,
T : Enum<T>
.
r

Ruckus

08/26/2019, 6:14 PM
Wouldn't that make
bind
infinitely recursive?
d

Dominaezzz

08/26/2019, 6:17 PM
Oh nvm. I just had another look at the signature of
bind
.
Should
bind
just be a member at this point.
e

elect

08/27/2019, 8:04 AM
@Ruckus interesting if I write:
fun <T> T.bind(target: BufferTarget) where T : Enum<T>, T : GlBufE = T.buffers[ordinal].bind(target)
I get:
Error:(182, 70) Kotlin: Type parameter 'T' cannot have or inherit a companion object, so it cannot be on the left hand side of dot
sorry, I'm an idiot:
fun <T> T.bind(target: BufferTarget) where T : Enum<T>, T : GlBufE = buffers[ordinal].bind(target)
👍 1
@Dominaezzz a member of what?
d

Dominaezzz

08/27/2019, 8:13 AM
Nvm
👍 1