Access Kotlin Standard Library from Swift












5















In my Kotlin Multiplatform project, I'm trying to access Kotlin types defined in kotlin-stdlib from Swift.



TL;DR: StdLib types/methods seem not to result in header definitions, I'd like a solution that doesn't involve writing lots of boilerplate code



My scenario



I have an interface defined in Kotlin ...



interface MyKotlinInterface {
fun run() : Sequence<String>
}


... and implemented this interface in Swift ...



class MySwiftClass : MyKotlinInterface {
func run() -> KotlinSequence {
// return sequenceOf("foo")
}
}


... there I'm trying to create a Sequence but there are no methods from the kotlin.sequences package available (e.g. generateSequence).



Is it actually possible to access Kotlin framework types or methods beyond what I define in my code -- if yes, how? Furthermore, how can this be achieved without writing boilerplate code?



Further details



Having a look into the generated Objective-C header file, I see definitions for my class (obviously) and basic Kotlin types. What's missing is basically everything from the standard library functionality (I care for everything Sequence-related).



My build.gradle.kts looks like:



plugins {
kotlin("multiplatform") version "1.3.0"
}

kotlin {
targets { /* ... */ }

sourceSets {

getByName("commonMain") {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib-common")
}
}

// ...

getByName("iosMain") {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib")
}
}
}
}


Having the kotlin-stdlib defined as a dependency for the iOS target, I would expect those to become actually available from Swift.



Minimal working example



https://github.com/panzerfahrer/so-mwe-kotlin-mpp-swift



Current solution approach



The only solution I came up with, is writing the desired function for the iOS target:



fun <T : kotlin.Any> generateSequence(nextFunction: () -> T?): kotlin.sequences.Sequence<T> = kotlin.sequences.generateSequence(nextFunction)


This works ok-ish but is highly unsatisfying as it requires lots of boilerplate code. Additionally, extension functions cannot be made available this way and would require more boilerplate code or even rewriting parts of the standard library.



Desired solution



I like to avoid writing boilerplate code as much as possible. What I actually only care about, is to have (in my case) Sequence fully accessible from Swift. My feeling is, it would be sufficient to make the compiler generate selected or all header definitions for the standard library functionality.










share|improve this question

























  • Meanwhile, I found a related Github issue that basically covers this case.

    – Brian
    Jan 23 at 9:31











  • Do you really need lazy computation (aka Sequence) in your Kotlin code? If no, I would recommend using List<T> instead (and it maps to Swift). For Sequence implementation, a workaround could be to export a factory function from you Kotlin library to help building a sequence on a Swift side

    – Eugene Petrenko
    Feb 14 at 17:54
















5















In my Kotlin Multiplatform project, I'm trying to access Kotlin types defined in kotlin-stdlib from Swift.



TL;DR: StdLib types/methods seem not to result in header definitions, I'd like a solution that doesn't involve writing lots of boilerplate code



My scenario



I have an interface defined in Kotlin ...



interface MyKotlinInterface {
fun run() : Sequence<String>
}


... and implemented this interface in Swift ...



class MySwiftClass : MyKotlinInterface {
func run() -> KotlinSequence {
// return sequenceOf("foo")
}
}


... there I'm trying to create a Sequence but there are no methods from the kotlin.sequences package available (e.g. generateSequence).



Is it actually possible to access Kotlin framework types or methods beyond what I define in my code -- if yes, how? Furthermore, how can this be achieved without writing boilerplate code?



Further details



Having a look into the generated Objective-C header file, I see definitions for my class (obviously) and basic Kotlin types. What's missing is basically everything from the standard library functionality (I care for everything Sequence-related).



My build.gradle.kts looks like:



plugins {
kotlin("multiplatform") version "1.3.0"
}

kotlin {
targets { /* ... */ }

sourceSets {

getByName("commonMain") {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib-common")
}
}

// ...

getByName("iosMain") {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib")
}
}
}
}


Having the kotlin-stdlib defined as a dependency for the iOS target, I would expect those to become actually available from Swift.



Minimal working example



https://github.com/panzerfahrer/so-mwe-kotlin-mpp-swift



Current solution approach



The only solution I came up with, is writing the desired function for the iOS target:



fun <T : kotlin.Any> generateSequence(nextFunction: () -> T?): kotlin.sequences.Sequence<T> = kotlin.sequences.generateSequence(nextFunction)


This works ok-ish but is highly unsatisfying as it requires lots of boilerplate code. Additionally, extension functions cannot be made available this way and would require more boilerplate code or even rewriting parts of the standard library.



Desired solution



I like to avoid writing boilerplate code as much as possible. What I actually only care about, is to have (in my case) Sequence fully accessible from Swift. My feeling is, it would be sufficient to make the compiler generate selected or all header definitions for the standard library functionality.










share|improve this question

























  • Meanwhile, I found a related Github issue that basically covers this case.

    – Brian
    Jan 23 at 9:31











  • Do you really need lazy computation (aka Sequence) in your Kotlin code? If no, I would recommend using List<T> instead (and it maps to Swift). For Sequence implementation, a workaround could be to export a factory function from you Kotlin library to help building a sequence on a Swift side

    – Eugene Petrenko
    Feb 14 at 17:54














5












5








5








In my Kotlin Multiplatform project, I'm trying to access Kotlin types defined in kotlin-stdlib from Swift.



TL;DR: StdLib types/methods seem not to result in header definitions, I'd like a solution that doesn't involve writing lots of boilerplate code



My scenario



I have an interface defined in Kotlin ...



interface MyKotlinInterface {
fun run() : Sequence<String>
}


... and implemented this interface in Swift ...



class MySwiftClass : MyKotlinInterface {
func run() -> KotlinSequence {
// return sequenceOf("foo")
}
}


... there I'm trying to create a Sequence but there are no methods from the kotlin.sequences package available (e.g. generateSequence).



Is it actually possible to access Kotlin framework types or methods beyond what I define in my code -- if yes, how? Furthermore, how can this be achieved without writing boilerplate code?



Further details



Having a look into the generated Objective-C header file, I see definitions for my class (obviously) and basic Kotlin types. What's missing is basically everything from the standard library functionality (I care for everything Sequence-related).



My build.gradle.kts looks like:



plugins {
kotlin("multiplatform") version "1.3.0"
}

kotlin {
targets { /* ... */ }

sourceSets {

getByName("commonMain") {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib-common")
}
}

// ...

getByName("iosMain") {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib")
}
}
}
}


Having the kotlin-stdlib defined as a dependency for the iOS target, I would expect those to become actually available from Swift.



Minimal working example



https://github.com/panzerfahrer/so-mwe-kotlin-mpp-swift



Current solution approach



The only solution I came up with, is writing the desired function for the iOS target:



fun <T : kotlin.Any> generateSequence(nextFunction: () -> T?): kotlin.sequences.Sequence<T> = kotlin.sequences.generateSequence(nextFunction)


This works ok-ish but is highly unsatisfying as it requires lots of boilerplate code. Additionally, extension functions cannot be made available this way and would require more boilerplate code or even rewriting parts of the standard library.



Desired solution



I like to avoid writing boilerplate code as much as possible. What I actually only care about, is to have (in my case) Sequence fully accessible from Swift. My feeling is, it would be sufficient to make the compiler generate selected or all header definitions for the standard library functionality.










share|improve this question
















In my Kotlin Multiplatform project, I'm trying to access Kotlin types defined in kotlin-stdlib from Swift.



TL;DR: StdLib types/methods seem not to result in header definitions, I'd like a solution that doesn't involve writing lots of boilerplate code



My scenario



I have an interface defined in Kotlin ...



interface MyKotlinInterface {
fun run() : Sequence<String>
}


... and implemented this interface in Swift ...



class MySwiftClass : MyKotlinInterface {
func run() -> KotlinSequence {
// return sequenceOf("foo")
}
}


... there I'm trying to create a Sequence but there are no methods from the kotlin.sequences package available (e.g. generateSequence).



Is it actually possible to access Kotlin framework types or methods beyond what I define in my code -- if yes, how? Furthermore, how can this be achieved without writing boilerplate code?



Further details



Having a look into the generated Objective-C header file, I see definitions for my class (obviously) and basic Kotlin types. What's missing is basically everything from the standard library functionality (I care for everything Sequence-related).



My build.gradle.kts looks like:



plugins {
kotlin("multiplatform") version "1.3.0"
}

kotlin {
targets { /* ... */ }

sourceSets {

getByName("commonMain") {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib-common")
}
}

// ...

getByName("iosMain") {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib")
}
}
}
}


Having the kotlin-stdlib defined as a dependency for the iOS target, I would expect those to become actually available from Swift.



Minimal working example



https://github.com/panzerfahrer/so-mwe-kotlin-mpp-swift



Current solution approach



The only solution I came up with, is writing the desired function for the iOS target:



fun <T : kotlin.Any> generateSequence(nextFunction: () -> T?): kotlin.sequences.Sequence<T> = kotlin.sequences.generateSequence(nextFunction)


This works ok-ish but is highly unsatisfying as it requires lots of boilerplate code. Additionally, extension functions cannot be made available this way and would require more boilerplate code or even rewriting parts of the standard library.



Desired solution



I like to avoid writing boilerplate code as much as possible. What I actually only care about, is to have (in my case) Sequence fully accessible from Swift. My feeling is, it would be sufficient to make the compiler generate selected or all header definitions for the standard library functionality.







android ios swift kotlin kotlin-multiplatform






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 12:41







Brian

















asked Nov 1 '18 at 15:05









BrianBrian

573924




573924













  • Meanwhile, I found a related Github issue that basically covers this case.

    – Brian
    Jan 23 at 9:31











  • Do you really need lazy computation (aka Sequence) in your Kotlin code? If no, I would recommend using List<T> instead (and it maps to Swift). For Sequence implementation, a workaround could be to export a factory function from you Kotlin library to help building a sequence on a Swift side

    – Eugene Petrenko
    Feb 14 at 17:54



















  • Meanwhile, I found a related Github issue that basically covers this case.

    – Brian
    Jan 23 at 9:31











  • Do you really need lazy computation (aka Sequence) in your Kotlin code? If no, I would recommend using List<T> instead (and it maps to Swift). For Sequence implementation, a workaround could be to export a factory function from you Kotlin library to help building a sequence on a Swift side

    – Eugene Petrenko
    Feb 14 at 17:54

















Meanwhile, I found a related Github issue that basically covers this case.

– Brian
Jan 23 at 9:31





Meanwhile, I found a related Github issue that basically covers this case.

– Brian
Jan 23 at 9:31













Do you really need lazy computation (aka Sequence) in your Kotlin code? If no, I would recommend using List<T> instead (and it maps to Swift). For Sequence implementation, a workaround could be to export a factory function from you Kotlin library to help building a sequence on a Swift side

– Eugene Petrenko
Feb 14 at 17:54





Do you really need lazy computation (aka Sequence) in your Kotlin code? If no, I would recommend using List<T> instead (and it maps to Swift). For Sequence implementation, a workaround could be to export a factory function from you Kotlin library to help building a sequence on a Swift side

– Eugene Petrenko
Feb 14 at 17:54












1 Answer
1






active

oldest

votes


















0














Do you really need lazy computation (aka Sequence) in your Kotlin code?
If no, I would recommend using List<T> instead (and it maps to Swift directly).



For Sequence implementation, a workaround could be to export a factory function from your Kotlin library, e.g. you may declare a function like



fun <T : kotlin.Any> generateSequence(nextFunction: () -> T?)
= kotlin.sequences.generateSequence(nextFunction)


You may select any other factory function for Sequence, that matches your use-case.



In general, there are too many functions in the Kotlin standard library. Exporting them all to Swift will create too many useless symbols in the binary and increase the compilation time.






share|improve this answer
























  • That's my current solution (see lower part of question) :-P

    – Brian
    Feb 18 at 20:49











  • Yep, maybe it makes sense to use List<T> not Sequence. Your example repo does not win from you using Sequence<T>

    – Eugene Petrenko
    Feb 19 at 13:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53103956%2faccess-kotlin-standard-library-from-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Do you really need lazy computation (aka Sequence) in your Kotlin code?
If no, I would recommend using List<T> instead (and it maps to Swift directly).



For Sequence implementation, a workaround could be to export a factory function from your Kotlin library, e.g. you may declare a function like



fun <T : kotlin.Any> generateSequence(nextFunction: () -> T?)
= kotlin.sequences.generateSequence(nextFunction)


You may select any other factory function for Sequence, that matches your use-case.



In general, there are too many functions in the Kotlin standard library. Exporting them all to Swift will create too many useless symbols in the binary and increase the compilation time.






share|improve this answer
























  • That's my current solution (see lower part of question) :-P

    – Brian
    Feb 18 at 20:49











  • Yep, maybe it makes sense to use List<T> not Sequence. Your example repo does not win from you using Sequence<T>

    – Eugene Petrenko
    Feb 19 at 13:52
















0














Do you really need lazy computation (aka Sequence) in your Kotlin code?
If no, I would recommend using List<T> instead (and it maps to Swift directly).



For Sequence implementation, a workaround could be to export a factory function from your Kotlin library, e.g. you may declare a function like



fun <T : kotlin.Any> generateSequence(nextFunction: () -> T?)
= kotlin.sequences.generateSequence(nextFunction)


You may select any other factory function for Sequence, that matches your use-case.



In general, there are too many functions in the Kotlin standard library. Exporting them all to Swift will create too many useless symbols in the binary and increase the compilation time.






share|improve this answer
























  • That's my current solution (see lower part of question) :-P

    – Brian
    Feb 18 at 20:49











  • Yep, maybe it makes sense to use List<T> not Sequence. Your example repo does not win from you using Sequence<T>

    – Eugene Petrenko
    Feb 19 at 13:52














0












0








0







Do you really need lazy computation (aka Sequence) in your Kotlin code?
If no, I would recommend using List<T> instead (and it maps to Swift directly).



For Sequence implementation, a workaround could be to export a factory function from your Kotlin library, e.g. you may declare a function like



fun <T : kotlin.Any> generateSequence(nextFunction: () -> T?)
= kotlin.sequences.generateSequence(nextFunction)


You may select any other factory function for Sequence, that matches your use-case.



In general, there are too many functions in the Kotlin standard library. Exporting them all to Swift will create too many useless symbols in the binary and increase the compilation time.






share|improve this answer













Do you really need lazy computation (aka Sequence) in your Kotlin code?
If no, I would recommend using List<T> instead (and it maps to Swift directly).



For Sequence implementation, a workaround could be to export a factory function from your Kotlin library, e.g. you may declare a function like



fun <T : kotlin.Any> generateSequence(nextFunction: () -> T?)
= kotlin.sequences.generateSequence(nextFunction)


You may select any other factory function for Sequence, that matches your use-case.



In general, there are too many functions in the Kotlin standard library. Exporting them all to Swift will create too many useless symbols in the binary and increase the compilation time.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 15 at 14:36









Eugene PetrenkoEugene Petrenko

2,6131825




2,6131825













  • That's my current solution (see lower part of question) :-P

    – Brian
    Feb 18 at 20:49











  • Yep, maybe it makes sense to use List<T> not Sequence. Your example repo does not win from you using Sequence<T>

    – Eugene Petrenko
    Feb 19 at 13:52



















  • That's my current solution (see lower part of question) :-P

    – Brian
    Feb 18 at 20:49











  • Yep, maybe it makes sense to use List<T> not Sequence. Your example repo does not win from you using Sequence<T>

    – Eugene Petrenko
    Feb 19 at 13:52

















That's my current solution (see lower part of question) :-P

– Brian
Feb 18 at 20:49





That's my current solution (see lower part of question) :-P

– Brian
Feb 18 at 20:49













Yep, maybe it makes sense to use List<T> not Sequence. Your example repo does not win from you using Sequence<T>

– Eugene Petrenko
Feb 19 at 13:52





Yep, maybe it makes sense to use List<T> not Sequence. Your example repo does not win from you using Sequence<T>

– Eugene Petrenko
Feb 19 at 13:52




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53103956%2faccess-kotlin-standard-library-from-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen