Reputation: 2255
The Code A works well, I think both Code B and Code C will work well too, but I failed, why?
In Code B and Code C, I think that the system can deduce the type of mDBHandle
Code A
class LogHandler(val mDBHandle:DBLogHandler=DBLogHandler()) {
}
class DBLogHandler() {
}
Code B
class LogHandler(val mDBHandle=DBLogHandler()) {
}
class DBLogHandler() {
}
Code C
class LogHandler(val mDBHandle:DBLogHandler()) {
}
class DBLogHandler() {
}
Upvotes: 0
Views: 63
Reputation: 3131
Quoting the reference:
Function parameters are defined using Pascal notation, i.e. name: type. Parameters are separated using commas. Each parameter must be explicitly typed
This also applies to constructors.
Let's look at your examples:
class LogHandler(val mDBHandle:DBLogHandler=DBLogHandler())
Here you declare a read-only property of type DBLogHandler
and a default value of a new instance (DBLogHandler()
).
class LogHandler(val mDBHandle=DBLogHandler())
In this one the parameter (property) type declaration has been omitted, so it's incorrect.
class LogHandler(val mDBHandle:DBLogHandler())
Here it looks like the parameter type has been declared as DBLogHandler()
, because the parentheses imply a function (or constructor) call. Therefore, it's not a valid declaration.
If you'd like to declare the mDBHandle
property without a default value, you can do it like this:
class LogHandler(val mDBHandle:DBLogHandler)
Upvotes: 3