breandan
02/06/2019, 3:51 PMEgor Trutenko
02/07/2019, 1:04 AMContainer<String>
and Container<Int>
.
A one particular solution (although not really flexible) would to subtype Container<T>
with statically typed StringContainer : Container<String>
and IntContainer : Container<Int>
. The problem is, as aforementioned, it's not really flexible and we have to do it by hand.
What if we could somehow tell compiler that we want to generate such subclasses with statically saved type parameter for every class that uses original, generic class? E.g.
open class Container<sticky T> {
...
}
...
val sc = Container<String>()
if (sc is Container<Int>) { // unreachable }
Under the hood would turn into
val sc = StringContainer()
if (sc is Container) { ... }
Theoretically, it is possible to track down all the types which generic type is parameterized with in compile-time; To avoid some problems with subclassing this functionality could be restricted for non-final classes. Also there could be problems with multiple type parameters.
So, thoughts? Would this be useful, what more problems can this bring, is there ways to enhance it?jossiwolf
02/07/2019, 11:53 AM@JvmOverloads
doesn't work in interfaces with default arguments? 🙂DALDEI
02/08/2019, 8:25 PMkevinmost
02/14/2019, 7:39 PM@JvmBuilder
compiler plugin in the stdlib + Kotlin IntelliJ plugin that would generate synthetic members to make builders on `data class`es for use in Java. Using `data class`es with many properties is currently pretty clunky in Java.
For example:
@JvmBuilder
data class Foo(val bar: String, val baz: Int = 0, val qux: String? = null)
should generate Foo.builder()
that returns an object like:
public final class FooBuilder {
public static FooBuilder builder() {
return new FooBuilder().baz(0).qux(null);
}
// fields...
public FooBuilder bar(String bar) { ... }
public FooBuilder baz(int baz) { ... }
public FooBuilder qux(@Nullable String qux) { ... }
public Foo build() {
// assert that properties with no default value were set
}
}
rrader
02/15/2019, 1:45 PMdata class Data(val a: Int)
Why Data::a
does not return KProperty<Data, Int>
by return KProperty<Int>
?
In first case we can write a function that receives only `Data`'s properties, something like func process(prop: KProperty<Data, Int>)
janvladimirmostert
02/19/2019, 5:45 AMlouiscad
02/21/2019, 10:44 AMtry registerAppInstance(info = AppInstanceInfo(
someToken = someValue
)) catch (e: HttpException) ui.showError(
when (val code = e.code()) {
503 -> ServiceUnavailableTryLater
...
else -> ...
}
)
Ruckus
02/23/2019, 5:27 AM?
on more and more operators
If a property is not null, it would be nice to have a conditional assignment operator that would assign a nullable value only if it is not null. For example:
map[KEY]?.let { value = it }
would become
value ?= map[KEY]
Edit
As @Dico pointed out, value ?= map[KEY]
can be a bit confusing, as it seems to imply value
is the nullable item instead of =
. It may make more sense to use value =? map[KEY]
, but I'm on the fence.groostav
03/13/2019, 8:07 PMval something = when(business) {
//...
someCase -> {
POKOType.forName(name) ?: Log.warning("angry"); POKOType()
}
If i had some kind of @Pure
ref-transparent enforcement I could at least get this code tagged as suspicious, if not generate a compiler failure out right.
whats the most aggressive thing I can do in my build system to catch this?galex
03/14/2019, 3:50 PMxenoterracide
03/15/2019, 4:10 PMJay
03/16/2019, 12:22 PMkarelpeeters
03/20/2019, 1:19 PMxenoterracide
03/20/2019, 5:47 PMlittlelightcz
03/22/2019, 5:57 PM@IWantNPEToBeThrownHere
🙂. Or perhaps the compiler could have a flag to turn this behaviour on.GreyhairRedbear
03/23/2019, 12:54 PM[weak self]
GreyhairRedbear
03/23/2019, 12:58 PMorangy
03/23/2019, 2:00 PMefemoney
03/28/2019, 5:16 PMpablisco
03/31/2019, 9:00 PMnatpryce
03/31/2019, 11:26 PMtschuchort
04/01/2019, 4:53 PMJorge Castillo
04/07/2019, 11:49 AMaerb
04/12/2019, 1:58 PMfun build(builder: (Context) -> View): View { TODO() }
val a = build(::View)
zokipirlo
04/17/2019, 8:22 AMraulraja
04/17/2019, 9:23 AMfo2rist
04/23/2019, 4:24 PMghedeon
05/08/2019, 11:31 PM<
operator for until
function. Gradually, having half-open and partial ranges, the slices syntax should follow.
Given:
Already implemented "closed range" in Kotlin.
Since Kotlin range is closed by default, by likeness, Groovy and Swift might be considered as an inspiration:
Closed range:
Swift: 0...5
Groovy: 0..5
Half-open range:
Swift: 0..<5
, ...<5
// compare to val a = until 5 ???
Groovy: 0..<5
Slice:
Swift: arr[1...5], arr[1..<5]
Groovy: arr[1..5]
======================================
Why it's important, why not until
, why partial range?
Personally, I believe that the closer we can get to the formal requirement (in this case — mathematical interval) the better. Thus, you don't execute the code but the requirement itself (it is only because of imperfection of our reality we're forced to write code around formal requirements 😉). That's why +
is better than plus()
. The former is essential and the latter is induced workaround.
Also imho, with KISS in mind, there is no need to reinvent slices syntax (I've seen :
proposal) when it's already covered by good range system. But it's a different discussion and I might be missing a bigger picture.
Does (1..<5)
look reasonable to you?
cc: @Dico, @Mike, @karelpeeters, @HullaballoonaticMarc Knaup
05/10/2019, 1:47 PMenum
type (instead of enum class
) then please don't do that 🙏Marc Knaup
05/10/2019, 1:47 PMenum
type (instead of enum class
) then please don't do that 🙏marstran
05/10/2019, 1:49 PMMarc Knaup
05/10/2019, 1:54 PMenum class
to be more specific.marstran
05/10/2019, 1:55 PMMarc Knaup
05/10/2019, 1:56 PMenum class
there could be a non-class enum
in the future 😄marstran
05/10/2019, 1:57 PMMarc Knaup
05/10/2019, 1:58 PMmarstran
05/10/2019, 2:02 PMMarc Knaup
05/10/2019, 2:10 PMordinal
, name
, valueOf
, Comparable
(and maybe others) defined by default which is not desirable.
• it has a recursively generic abstract superclass.
A String would be a reference type again, including related overhead.
In memory it could be an primitive int, a memory address or whatever - I don't care nor need to know as a developer. Similar to inline classes and properly implemented in the JVM with project Valhalla.r4zzz4k
05/10/2019, 2:22 PMComparable
is expected on these or not, but regarding primitives and enums you can check this: https://youtrack.jetbrains.com/issue/KT-23823Ruckus
05/10/2019, 2:31 PMfun log(level: Level, message: String) {
if (level > threshold) ...
}
Marc Knaup
05/10/2019, 2:55 PMRuckus
05/10/2019, 3:15 PMto ascertain the number of; to specify one after another
An enumeration is a complete, ordered listing of all the items in a collection.
Marc Knaup
05/10/2019, 3:26 PMRuckus
05/10/2019, 3:28 PMMarc Knaup
05/10/2019, 3:31 PMlistOf(2, 1)
1 comes after 2 its not automatically greater than 2.Ruckus
05/10/2019, 3:36 PMMarc Knaup
05/10/2019, 3:36 PMRuckus
05/10/2019, 3:37 PMMarc Knaup
05/10/2019, 3:43 PMjeremy
05/10/2019, 3:47 PMMarc Knaup
05/10/2019, 3:50 PMRuckus
05/10/2019, 3:50 PMsealed class
). If it doesn't make sense to say South comes after North, it doesn't make sense to enumerate it.Marc Knaup
05/10/2019, 3:52 PMRuckus
05/10/2019, 3:54 PMMarc Knaup
05/10/2019, 3:56 PMenum class
in Kotlin works the way it does is that it needed Java compatibility.
I just suggest rethinking it as Kotlin becomes more and more its own language.Ruckus
05/10/2019, 4:00 PMAny
doesn't implement the same API as Java's object
. I would counter that enum class
in Kotlin works the way it does because it makes sense. That's why most every language with enums does that, even if they never talk to Java or C or whatever.Marc Knaup
05/10/2019, 4:03 PMRuckus
05/10/2019, 4:05 PMMarc Knaup
05/10/2019, 4:07 PMRuckus
05/10/2019, 4:11 PMMarc Knaup
05/10/2019, 4:14 PMname
because it refers to Java/Kotlin identifier (the enum case) instead of the name of the thing the enum case actually describesRuckus
05/10/2019, 4:15 PMMarc Knaup
05/10/2019, 4:16 PMRuckus
05/10/2019, 4:16 PMname
is completely orthoganal to the comparability discussion.Marc Knaup
05/10/2019, 4:16 PMRuckus
05/10/2019, 4:17 PMCharSequence
CharSequence
is comparableMarc Knaup
05/10/2019, 4:19 PMRuckus
05/10/2019, 4:19 PMMarc Knaup
05/10/2019, 4:20 PMRuckus
05/10/2019, 4:20 PMMarc Knaup
05/10/2019, 4:22 PMlouiscad
05/10/2019, 4:42 PMenum
.
That said, what you are looking for is sealed class
of `object`s, with explicit inlining where the ordinal used for the underlying constant primitive is explicitly specified but private to the declaration, so it can be used for public APIs without unexpected breakages.
We could also imagine a set class
that would be like a one level sealed class
with stateless objects only, where either order would matter, or explicit ordinal would be specified, so the compiler flattens it to primitive values for the produced binary.
It could also work with something like R8 or proguard with a Kotlin specific logic to inline down to primitive all fully stateless object
based `sealed class`es.
Are you targetting Android?Marc Knaup
05/10/2019, 4:44 PMDico
05/10/2019, 9:18 PMenum class
, each of which have their use cases, you should consider using another way of declaring it.louiscad
05/11/2019, 7:59 PMDico
05/11/2019, 8:14 PMMarc Knaup
05/12/2019, 11:27 AMDico
05/12/2019, 11:40 AMMarc Knaup
05/12/2019, 11:42 AMDico
05/12/2019, 11:43 AMMarc Knaup
05/12/2019, 11:44 AMDico
05/12/2019, 11:45 AMMarc Knaup
05/12/2019, 11:46 AMDico
05/12/2019, 11:46 AMlouiscad
05/12/2019, 11:46 AMsealed class
. There's even an IDE intention to do the conversion in no time.Marc Knaup
05/12/2019, 11:46 AMlouiscad
05/12/2019, 11:48 AMMarc Knaup
05/12/2019, 11:48 AMlouiscad
05/12/2019, 11:50 AMMarc Knaup
05/12/2019, 11:52 AMlouiscad
05/12/2019, 11:52 AMMarc Knaup
05/12/2019, 11:54 AMlouiscad
05/12/2019, 11:55 AMString
, but not an enum of String
, this doesn't make sense and is impossible. The name of the enum entry though, is a String
, and you can also retrieve it.Marc Knaup
05/12/2019, 11:55 AMlouiscad
05/12/2019, 11:55 AMMarc Knaup
05/12/2019, 11:56 AMlouiscad
05/12/2019, 11:58 AMenum class
.Marc Knaup
05/12/2019, 12:00 PMlouiscad
05/12/2019, 12:01 PMMarc Knaup
05/12/2019, 12:01 PMlouiscad
05/12/2019, 12:05 PMMarc Knaup
05/12/2019, 12:11 PMDico
05/12/2019, 1:45 PMlouiscad
05/12/2019, 2:10 PMallCases
property defined later on.
2. To have something like `sealed class`es, they use the keyword indirect
to have an enum case be another enum, which reads harder than it should be IMHO.
3. Their "enums" are really just a set/group of values that enables genuinely cool syntax in `switch`es and the related compiler checks, and you can make them use any primitive value, be it a Character
, a Float
, etc.
Consequently, I think we're good in Kotlin with `sealed class`es, Java's and Kotlin enums are more accurate to the English definition of enumeration with their comparability, ordinals and names, and are also simpler although less full featured than Swift's fake enums.
I'm not against something like in Swift as kind of a middle ground between `sealed class`es and `enum class`es, but I don't want them to be referred as enumerations. `set class`es is one of the naming I have in mind. If you think such a thing should be introduced in Kotlin because you have or can find use cases for it that current options don't satisfy well, then starting to work on a KEEP would be sound thing to do.gildor
05/14/2019, 7:09 AMkind of a middle ground between `sealed class`es andoh, please, no need to add new entities to the language, some people already confused what to choose enum or sealed classenum class