Access Kotlin Standard Library from Swift











up vote
5
down vote

favorite












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 as much boilerplate code 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




























    up vote
    5
    down vote

    favorite












    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 as much boilerplate code 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


























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      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 as much boilerplate code 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 as much boilerplate code 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.







      ios swift kotlin kotlin-multiplatform






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 15:18

























      asked Nov 1 at 15:05









      Brian

      569920




      569920





























          active

          oldest

          votes











          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',
          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





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          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




















































































          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)