Why some functions in stdlib start with a capital ...
# stdlib
d
Why some functions in stdlib start with a capital letter when the name matches return type (as described in the coding conventions) but some don’t? For example, uppercase
List
but lowercase `sequence`:
Copy code
List(size = 1) { "foo" }
sequence { yield("foo") }
m
sequence is a builder/DSL. List is simply passed a constructor parameter that is called for each index.
d
DSL or not they are both functions:
Copy code
public inline fun <T> List(...): List<T> = ...
public fun <T> sequence(...): Sequence<T> = ...
e
I don't know if this is the reason, but: List(n) { ... } is clearly a constructor or function invocation Runnable { ... } is clearly a lambda sequence { ... } is clearly a function invocation if it were Sequence { ... }, that is not so clear
👆 1
j
That does make a lot of sense.
kotlin/collections/Sequences.kt
contains the following though--
Copy code
public inline fun <T> Sequence(crossinline iterator: () -> Iterator<T>): Sequence<T> = object : Sequence<T> {
    override fun iterator(): Iterator<T> = iterator()
}
e.g. the implementation of
fun sequence(..)
is just
Sequence { iterator(block) }
, calling
fun Sequence(..)
this is the one which I’m most curious about!
☝️ 1
(I mean the answer might be that it’s a wordy way of making
interface Sequence
into effectively
fun interface Sequence
, but why guess when you can ask? 🙂)
d
@ephemient
List(n) { ... } is clearly a constructor or function invocation
Yes.
Runnable { ... } is clearly a lambda
Not unless you know that Runnable is a functional interface, so I wouldn’t say “clearly” 😉
sequence { ... } is clearly a function invocation
Yes. The question is why make it clearly a function and also have
fun Sequence(){...}
. It would be a more consistent coding style in stdlib to capitalise all functions if they have the same name as returning type.
e
I didn't realize fun Sequence() was public, but it acts constructor-like while sequence() acts builder-like, it doesn't feel like the same thing
now I do think it would be nice to have all the builders named similarly, e.g. stdlib's
buildString { ... }
buildList { ... }
etc., but there was a community poll a little while back where people seemed to mostly prefer the non-build-prefixed names so...
m