Alexis Manin
10/07/2022, 8:13 AM..<
operator. Will other mathematical range operators considered in the future ? Like open started (<..
?) Ranges, or fully open ranges (<..<
?) ? I think both are not very common, but maybe it would make sense to be exhaustive in this matter ? Maybe some mathematical libraries would have use for it ?dmitriy.novozhilov
10/11/2022, 7:36 AMT
used in receiver then it's always possible to infer T
from it (because all types in receiver should be inferred before call/access itself)
But if T
is used only in return type then there will be cases when compiler can not infer it, like
val x = something // no information to infer T
janvladimirmostert
10/11/2022, 8:31 PMcontext(A, B, C, D, E, F, G, H, I, J)
someFunction() { ... }
with (a) {
with (b) {
with (c) {
with (d) {
with (e) {
...
with(j) {
someFunction()
}
}
}
}
}
}
I was thinking with (a & b & c & d & e & g & h & i & j)
or contexts (a, b, c, d, e, g, h, i, j)
that automatically generates that nested withs, but that's even messier
obviously I've exaggerated the number of params, but if context receivers were to be used as a replacement for dependency injection (which seems like a brilliant use-case),
I can easily see the number of params increasing as the number of services in a project increases.
For now, the obvious workaround is just stashing all those fields in a single class and doing something like this:
context(Alphabet)
fun someFunction() {
this@Alphabet.a.doSomethingWithA();
}
with(Alphabet(a = TODO(), b = TODO(), ..., z = TODO())) {
someFunction()
}
Maybe this can become
context class Alphabet(
val a: A
val b: B
...
val z: Z
)
now if you
context(Alphabet)
, it puts all of those fields in context instead of the Alphabet class itself like a data class would have done.
Also, if something has context(A)
, I can just pass the whole Alphabet
context instead of wrapping the call-site in with (this@Alphabet.a) { ... }
since Alphabet already contains A
Kristian Nedrevold
10/16/2022, 10:24 AMMarc Knaup
10/16/2022, 11:03 AM@NoInfer
being standardized. Any plans on that?
Often you have to provide something like someDefinition<Type> {…}
or resolve<Type> {…}
- where you define something for a specific type. For example:
• In dependency injection I want to force an explicit type to be provided. It’s too easy to accidentally infer a wrong type (esp. after refactoring something) and it’ll only result in an error sometime at runtime.
• BSON / GraphQL type definitions. Here I want to be explicit for safety. Also, definition blocks can become quite long and if you have many across your project figuring out the type takes a while because it’s implicit. Last but not least type inferences seems to slow down compiler and IDE performance significantly.elect
10/21/2022, 9:38 AMNumber
@JvmInline
public value class UInt @PublishedApi internal constructor(@PublishedApi internal val data: Int) : Comparable<UInt>, Number by data
and I'd be super happy because I don't have to create tons of additional and redundant constructors and methods specifically for every single unsigned (in glm)Ben Woodworth
10/22/2022, 2:46 PMcontext
here is also an error, but just not reported as one yet since it's a prototype/experimental)elect
10/25/2022, 6:58 AMclass Vec2(val x: Float, val y: Float) {
inline fun plus(s: Float, res: (resX: Float, resY: Float) -> Unit) {
kotlin.contracts.contract { callsInPlace(res, kotlin.contracts.InvocationKind.EXACTLY_ONCE) }
res(x + s, y + s)
}
}
This code can then be called as
val a = Vec2(0, 1)
val b: Float; val c: Float
a.plus(3) { x, y -> b = x; c = y }
it's useful in hot loops (in 3d real time graphics) or, in Louis' example in Android, where you want to save any allocation you can
Now, It'd be nice if Kotlin may automatically destructurize a lambda placed at the last place in a function signature without any allocations. That is something like:
val a = Vec2(0, 1)
val (b, c) = a.plus(3) { x, y }
while underneath the compiler would automatically apply the code as shown in the first example
Another nice advantage, which would come for "free", it's essentially using an argument parameter as a return one, overcoming the JVM limitation of not considering the return type as part of the function signaturedave08
11/01/2022, 1:06 PMinner object
declaration? Something similar to:
class Foo {
val num = 1
inner class Bar {
val one = "something $num"
}
val bar = Bar()
init {
// use it:
println(bar.one)
}
}
Emil Kantis
11/09/2022, 2:58 PMobject : MultipleElementDecoder<List<T>> {
context(ByteArrayReader)
override fun decode(numberOfElements: Int): List<T> {
return (1..numberOfElements).map { readUntil('/').decodeNumericRange(block) }
}
}
with this lambda:
MultipleElementDecoder { numberOfElements -> (1..numberOfElements).map { readUntil('/').decodeNumericRange(block) } }
But then it complains.. Not even sure what to start searching for in Youtrack.. 😞elect
11/11/2022, 11:07 AMoperator fun ByteBuffer.get(index: Int): Byte = ..
operator fun ByteBuffer.get(index: Int): Int = ..
Swift does that and it's great and the JVM underneath does include the function return type in the signaturethemishkun
11/14/2022, 10:36 AMade
11/16/2022, 9:03 AMprivate fun String.hashCode() = 42
Emil Kantis
12/02/2022, 10:02 AMsealed interface
should be data types? sealed data interface
could perhaps be a nice addition? I would like to use copy
with the subtypes.. 🙂 apologies for the low-effort proposal.. just want to get some discussion ✌️hfhbd
12/04/2022, 11:40 AMMichael Böiers
12/07/2022, 8:57 AMcopy
method in all classes that meet the necessary requirements - not just in data classes.
A good use case for that are database entities. They have a lot of attributes, and if you model the entities as immutable (which I like to do), you really need to define them as data classes just to get the copy method. But since database tables can have cyclical references, you then need to remember to override hashcode, equals and toString to get back to default behavior. Otherwise you will run into stack overflow errors at runtime. That kind of sucks, when all I ever needed was the damn copy method!Michael Böiers
12/08/2022, 9:56 AMIterable<T>.takeUntil(pred: (T) -> Boolean)
or else, to make semantics more explicit, let’s add an optional parameter to takeUnless:
list.takeUnless(includeBreakingElement = true) { … }
or how about this:
list.takeUntilIncluding { … }
to that one we could also add an optional parameter indicating what should happen if no element matches. Callers could either want the entire list/stream returned, or an empty list (not possible for streams/sequences).Emil Kantis
12/09/2022, 2:45 PMobject ColorEncoder : Encoder<Color> by { Encoder { appendable, t -> appendable.appendColor(t) }}()
But inlining the lambda body to this doesnt:
object ColorEncoder : Encoder<Color> by Encoder { appendable, t -> appendable.appendColor(t) }
(I get that we can use a val instead, but would like to use objects to keep things consistent)xxfast
12/13/2022, 11:15 PMzipWithNull
(as seen on #advent-of-code)
fun <T, R> Iterable<T>.zipWithNull(other: Iterable<R>): List<Pair<T?, R?>> = buildList {
val first = this@zipWithNull.iterator()
val second = other.iterator()
while (first.hasNext() || second.hasNext()) {
val left = if (first.hasNext()) first.next() else null
val right = if (second.hasNext()) second.next() else null
add(left to right)
}
}
Michael de Kaste
12/15/2022, 8:19 AM1..3 + 2..4 + 6..8 = Multirange(1..4, 6..8)
vngantk
12/17/2022, 5:24 PMcorneil
12/18/2022, 11:41 AMEndre Deak
12/19/2022, 7:00 PMcopy
method in place?mattinger
01/11/2023, 5:47 PMcopy
and destructuring functions which can cause backward compatibility issues in libraries. I was pointed to the https://github.com/drewhamilton/Poko library by someone, but i’m reluctant to use compiler plugins if i don’t have to, due to the current lack of a stable api which leads these plugins to have to adapter to the ever evolving plugin api.elect
01/26/2023, 10:29 AMNavInput._DpadLeft !_isTest readMode
dave08
02/01/2023, 11:52 AMfun interface Foo { ... }
// this works:
data class Baz(val foo: Foo)
val baz = Baz({ ... })
// whereas this doesn't...
fun baz(foo: Foo = { ... })
// it needs to be:
fun baz(foo: Foo = Foo { })
why doesn't the compiler infer Foo as the lambda type in a default value of a function declaration?Endre Deak
02/15/2023, 8:51 PMcopy
method?
data class Foo(val bar: String)
val foo = Foo("bar")
foo.copy(bar = "bar1") // current
// could be this:
foo.copy {
bar = "bar1"
}
Slackbot
02/22/2023, 12:37 PMMichael de Kaste
03/03/2023, 12:37 PMsealed interface Range<T : Comparable<T>>{
interface BoundedLeft<T : Comparable<T>> : Range<T>
}
val range: Range.BoundedLeft<LocalDate>
typealias DateRange = Range<LocalDate>
although the above statement is how we like to use it, the bottom two lines can't work:
val daterange: DateRange.BoundedLeft (Unresolved reference: BoundedLeft)
typealias DateRange.BoundedLeft = Range.BoundedLeft<LocalDate> (Redeclaration: DateRange)
simon.vergauwen
03/06/2023, 2:04 PMsuspend
lambda?simon.vergauwen
03/06/2023, 2:04 PMsuspend
lambda?hfhbd
03/06/2023, 4:51 PMexample
?Joffrey
03/06/2023, 5:07 PMblock
lambda in example()
, it is expected by the lambda. So example()
is actually supposed to create it and pass it to the lambda, not the other way around.example(::one)
instead of forcing example { one() }
if one
doesn't use the context.hfhbd
03/06/2023, 5:10 PMblock
inside example without a context provided?
fun <A> example(block: context(Fragment, View) () -> A): A { block(); TODO() }
Checking if the context is actually used could only work inside the module. Otherwise, a new version of the lib actually using the context would break the public api.Joffrey
03/06/2023, 5:12 PMblock
will be given a context (if called). So example
has to provide one when it calls block()
. That is irrelevant to whether callers of example
pass a lambda that actually uses itexample { one() }
This could be simplified to example(::one)
but the compiler is a bit too strict about the function signature to realize that it's ok.suspend
keyword is kind of a subtype of the same function type with a suspend
keyword.hfhbd
03/06/2023, 5:22 PMelizarov
03/07/2023, 8:07 AMsimon.vergauwen
03/07/2023, 8:10 AM