Andrew Gazelka
04/17/2019, 7:58 PMkevinmost
04/17/2019, 8:12 PMdata class Duration private constructor(val millis: Long)
?Andrew Gazelka
04/17/2019, 8:16 PMShawn
04/17/2019, 8:16 PMcopy()
Andrew Gazelka
04/17/2019, 8:16 PMShawn
04/17/2019, 8:17 PMinternal constructor
?Andrew Gazelka
04/17/2019, 8:17 PMShawn
04/17/2019, 8:18 PMShawn
04/17/2019, 8:18 PMAndrew Gazelka
04/17/2019, 8:19 PMAndrew Gazelka
04/17/2019, 8:19 PMAndrew Gazelka
04/17/2019, 8:44 PMAndrew Gazelka
04/17/2019, 8:44 PMstreetsofboston
04/17/2019, 8:44 PMAndrew Gazelka
04/17/2019, 8:44 PMbdawg.io
04/17/2019, 8:44 PMprivate
- Pros: Restricts instance creation within the object itself
- Cons: Restricts instance creation with the object itself (this will force you to have those extension values to delegate the creation of a Duration within a companion object or some other nested object)
data class Duration private constructor(val milis: Long) {
companion object {
fun fromIntMillis(source: Int) = Duration(source.toLong())
}
}
val Int.millis get() = Duration.fromIntMillis(this)
2) only provide a public interface of your Duration (as you originally proposed) and have a private implementationbdawg.io
04/17/2019, 8:45 PMbdawg.io
04/17/2019, 10:24 PMAndrew Gazelka
04/17/2019, 11:23 PMAndrew Gazelka
04/17/2019, 11:24 PMAndrew Gazelka
04/17/2019, 11:24 PMbdawg.io
04/18/2019, 5:55 AM