https://kotlinlang.org logo
#announcements
Title
# announcements
r

Robert

10/09/2019, 8:30 PM
If I use a
var
or
val
in an interface, do I always have to use
override
to "implement" it? I tried using
abstract
in the
interface
, but that's no help
s

Sam Schilling

10/09/2019, 8:36 PM
What is your goal? To avoid the
override
keyword? You need that to indicate you are defining/initializing a value that comes from the interface
r

Robert

10/09/2019, 8:37 PM
Basically, yes. I chose this over a constructor with named parameters, but the override is making it more complex that I'd like to
Same thing for abstract class
Copy code
abstract class testA {
    abstract val test: Int
}

class testB: testA() {
    override val test: Int = 1
}
I have like 20+ properties which should all have a value
s

Sam Schilling

10/09/2019, 8:40 PM
you can implement the property in the interface using accessors, but in that case the implementation would be the same across multiple inheritors. Otherwise you’ve got to use
override
.
r

Robert

10/09/2019, 8:42 PM
Hmm, maybe I'd better switch to named constructor parameters then, or use data classes
s

Sam Schilling

10/09/2019, 8:42 PM
Why the hesitancy for using
override
?
It’s not as if it makes the code more complex, it just makes it more explicit that the properties come from an interface
r

Robert

10/09/2019, 8:59 PM
Yeah, just visual. I know have 20 lines with
override var
. Just would look cleaner without it, that's basically it.
s

Sam Schilling

10/09/2019, 9:01 PM
Regardless you are going to have 20 lines with
var
, the
override
just helps distinguish from other variables that aren’t from the interface. I don’t think it makes it look less clean, it makes it more explicit. I think the unclean aspect is moreso the fact that you have 20 vars to begin with, but that may be unavoidable depending on your use case.
r

Robert

10/09/2019, 9:03 PM
Yeah well, it looks kinda shit tbh 😉
Copy code
override val consensus by lazy {
        object: Params {
            override var nSubsidyHalvingInterval = 210000
            override var BIP16Exception = SHA256("0x00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22");
            override var BIP34Height = 227931u
            override var BIP34Hash = SHA256("0x000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8");
            override var BIP65Height = 388381u
            override var BIP66Height = 363725u
            override var CSVHeight = 419328u
            override var SegwitHeight = 481824u
            override var nMinerConfirmationWindow = 2016u // nPowTargetTimespan / nPowTargetSpacing
            override var MinBIP9WarningHeight = this.SegwitHeight + this.nMinerConfirmationWindow;
            override var powLimit = SHA256("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
            override var nPowTargetTimespan = 14u * 24u * 60u * 60u
            override var nPowTargetSpacing = 10u * 60u
            override var fPowAllowMinDifficultyBlocks = false
            override var fPowNoRetargeting = false
            override var nRuleChangeActivationThreshold = 1916 // 95% of 2016
            override val vDeployments = mapOf(
                DeploymentPos.DEPLOYMENT_TESTDUMMY to BIP9Deployment(
                    28u,
                    1199145601u, // January 1, 2008
                    1230767999u // December 31, 2008
                )
            )
            override var nMinimumChainWork = SHA256("0x000000000000000000000000000000000000000008ea3cf107ae0dec57f03fe8")
            override var defaultAssumeValid = SHA256("0x00000000000000000005f8920febd3925f8272a6a71237563d78c2edfdd09ddf") // 597379
        }
    }
Guess named parameters are cleaner
I'll just go with that
s

Sam Schilling

10/09/2019, 9:05 PM
I don’t think removing the overrides makes it look much better tho lol
🤣 1
😆 2
But yeah, named parameters are one way to go about it
👌 1
And in this case would probably help it look more clean
r

Robert

10/09/2019, 9:05 PM
Yeah, the naming will change once I refactor it, that'll make it a little more shitty
s

Sam Schilling

10/09/2019, 9:08 PM
Also, if you want to make things look a little cleaner I’d recommend following the style guide for properties https://kotlinlang.org/docs/reference/coding-conventions.html#property-names
r

Robert

10/09/2019, 9:09 PM
Yes, I am just migrating it 1:1 for now, and will refactor it to okayish names once the code is complete 👍
👍 1
Ty