https://kotlinlang.org logo
#k2-early-adopters
Title
# k2-early-adopters
n

Nick

12/13/2023, 3:59 PM
I'm hitting KT-57614 with code that exposes DOM types in common code. Doodle uses this approach currently, and relies on aliasing for the actual type in JS. Unfortunately, none of the work-arounds mentioned in the issue work b/c I don't control the definition for the impl type I'm aliasing. So removing the default value from my expect statement simply raises new errors:
Copy code
e: file:///Users/neddy/Development/Projects/doodle/Browser/src/jsMain/kotlin/io/nacular/doodle/HTMLElement.kt:116:27 Actual class 'Document' has no corresponding members for expected class members:

    internal final expect fun createElement(localName: String, options: ElementCreationOptions /* = ElementCreationOptions */): Element /* = Element */

    The following declaration is incompatible because actual function cannot have default argument values, they should be declared in the expected function:
        public final fun createElement(localName: String, options: ElementCreationOptions = ...): Element

    internal final expect fun createElementNS(namespace: String?, qualifiedName: String, options: ElementCreationOptions /* = ElementCreationOptions */): Element /* = Element */

    The following declaration is incompatible because actual function cannot have default argument values, they should be declared in the expected function:
        public final fun createElementNS(namespace: String?, qualifiedName: String, options: ElementCreationOptions = ...): Element
Is there some solution to make this work for aliasing impls with default parameters that cannot be changed?
k

kirillrakhman

12/13/2023, 4:05 PM
@Roman Efremov
r

Roman Efremov

12/14/2023, 10:40 AM
In this case, the possible solution would be to replace typealias with a real class, and use typealiased class 1. Either with inheritance:
Copy code
public actual class Document : org.w3c.dom.Document() {
But in such case you will hit "Overriding 'external' function with optional parameters", which you can fix by renaming methods
createElement
and
createElementNs
, if it's an option. 2. …or, if it's not an option, you can use delegation:
Copy code
public actual class Document {
    private val delegate = org.w3c.dom.Document()
    
    internal actual fun createElement(localName: String, options: ElementCreationOptions): Element {
        return delegate.createElement(localName, options)
    }
👍 1