Using Sequence<T> from Java.

kotlin · · 292 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have an API built in Kotlin and I am testing out the Java interop story, but have run into issues with the <code>Sequence&lt;T&gt;</code> type.</p> <p>The Kotlin API has a function that returns <code>Sequence&lt;T&gt;</code>, and generally when implementing the function under Kotlin, you&#39;d do something like:</p> <pre><code>override fun next(): Sequence&lt;Instruction&lt;T&gt;&gt; = buildSequence { while (true) { yield(this@BaseInstructionGenerator.generateRandomInstruction()) } } </code></pre> <p>However -- if implementing the function in Java -- I am unsure how to return a <code>Sequence&lt;T&gt;</code>. I can&#39;t seem to find any documentation on this, and I can&#39;t find a way to convert some other type (e.g. <code>Iterable&lt;T&gt;</code>) to <code>Sequence&lt;T&gt;</code>.</p> <p>Does anyone know how this can be achieved? If it is not possible, then I will simply change the Kotlin API to return an <code>Iterable&lt;T&gt;</code>.</p> <hr/>**评论:**<br/><br/>hpernpeintner: <pre><p>Could asSequence help you here? <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/as-sequence.html" rel="nofollow">https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/as-sequence.html</a></p></pre>QshelTier: <pre><p>Hmm. Judging from the Kotlin source code, <code>kotlin.sequences.Sequence</code> is a plain interface with an <code>iterator()</code> method. You shouldn’t have any problems using that (i.e. implementing it) from Java.</p></pre>
292 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet