marcusfs
11/28/2018, 3:10 PMsealed data class Foo {
First,
Second(val x: Int),
Third(val s: String)
}
instead of this:
sealed class Foo {
object First : Foo()
data class Second(val x: Int) : Foo()
data class Third(val s: String) : Foo()
}
it's not a huge difference but it feels unnecessarily messy to do it the current way (especially when you mix in "no argument" data classes and have to use object/class instead) , and I imagine this is a pretty common use-case for sealed classes. what do you think?orangy
enum
, all enum values are singletons.marcusfs
11/28/2018, 3:20 PMorangy
values
, valueOf
, etc, that assumes all variants are singletons. Using same word for something that is significantly different is very confusing.marcusfs
11/28/2018, 3:23 PMorangy
enum
comes from the fact that all values are enumerable
, after all.marcusfs
11/28/2018, 3:24 PMorangy
:Foo()
and data class
?marcusfs
11/28/2018, 3:25 PMobject
if you don't have any arguments to a caseclass
i guess)orangy
marcusfs
11/28/2018, 3:29 PMsealed class AddEditTaskEvent {
data class DefinitionCompleted(val title: String, val description: String) : AddEditTaskEvent()
object CreatedSuccessfully : AddEditTaskEvent()
data class CreationFailed(val reason: String) : AddEditTaskEvent()
object UpdatedSuccessfully : AddEditTaskEvent()
data class UpdateFailed(val reason: String) : AddEditTaskEvent()
}
versus
enum data class AddEditTaskEvent {
DefinitionCompleted(val title: String, val description: String),
CreatedSuccessfully,
CreationFailed(val reason: String),
UpdatedSuccessfully,
UpdateFailed(val reason: String)
}
orangy
enum
is appropriate shortcut here.marcusfs
11/28/2018, 3:39 PMkarelpeeters
11/28/2018, 11:30 PMenum
in rust, which misses the JVM concept of enum entirely)gildor
11/29/2018, 3:12 AMsealed class AddEditTaskEvent
data class DefinitionCompleted(val title: String, val description: String) : AddEditTaskEvent()
object CreatedSuccessfully : AddEditTaskEvent()
data class CreationFailed(val reason: String) : AddEditTaskEvent()
object UpdatedSuccessfully : AddEditTaskEvent()
data class UpdateFailed(val reason: String) : AddEditTaskEvent()
And used as:
CreationFailed("reason)
instead of
AddEditTaskEvent.CreationFailed
But with implicit sealed class you don’t have such choice.marcusfs
11/29/2018, 9:17 AMgildor
11/29/2018, 9:20 AMEvent.Success
vs SuccessEvent
, you don’t want to have Event.SuccessEvent
, I just pointed that this proposal doesn’t work well with non-nested classesmarcusfs
11/29/2018, 9:20 AMenum
(or enum-with-associated-data as they refer to it) works like rust too. one really frustrating thing about their enum though is that the cases aren't subtypes of the enum type - in fact they are not even types at all and you can only access them using pattern matching.enum Foo {
case First
case Second(Int)
case Third(String)
}
Rust
enum Foo {
First,
Second(int),
Third(String)
}
Haskell
data Foo = First | Second Int | Third String
sealed data classes already have the right semantics for this, it's just that i think the syntax when declaring it is a bit verbose, as i stated abovegildor
11/29/2018, 9:35 AMFirst
, not Foo.First
. i don’t think this would be useful for every use-caseThat’s the problem, you suggest new language feature and new syntax, which is big change itself, so should work for so many cases as it possible Maybe there are some other ways to avoid additional verbosity, but now it’s pretty limited, for example you also want to have constructor properties for parent sealed class, but this syntax doesn’t allow that
marcusfs
11/29/2018, 9:44 AMgildor
11/29/2018, 9:46 AMmarcusfs
11/29/2018, 9:47 AMgildor
11/29/2018, 9:47 AMmarcusfs
11/29/2018, 9:48 AMgildor
11/29/2018, 9:49 AMmarcusfs
11/29/2018, 9:57 AMdata class
is shorthand for a specific kind of class
(namely a product type), i'd really like to see something like a sealed data class
as a complement for supporting sum types.gildor
11/29/2018, 10:00 AMmarcusfs
11/29/2018, 10:01 AMgildor
11/29/2018, 10:01 AM, i’d really like to see something like athis actually interesting addition, maybe this makes more sense, because doesn’t clash with existing implementationsealed data class
marcusfs
11/29/2018, 10:06 AMorangy
sealed data class
clashes with existing meaning. E.g. if you have inner class
and you make it inner data class
it doesn’t change syntax that radicallymarcusfs
11/29/2018, 10:09 AMorangy
marcusfs
11/29/2018, 10:13 AMgildor
11/29/2018, 10:24 AMeven enums allow bodies after allBut the same body for each item, which may be too restrictive for sealed class (because each item has own types in constructor, so probably want to have own methods)
marcusfs
11/29/2018, 10:25 AMDico
11/29/2018, 10:26 AMsealed data class
. Body of the parent class can follow the ;
just like in enum class (even if I'm not personally a fan of that syntax)enum class
you call the parent constructor instead of declaring a new onemarcusfs
11/29/2018, 10:35 AMlouiscad
11/29/2018, 9:20 PMgildor
11/30/2018, 1:05 AM