namespace addressbook class Contact( val name : String, val emails : List<EmailAddress>, val addresses : List<PostalAddress>, val phonenums : List<PhoneNumber> ) class EmailAddress( val user : String, val host : String ) class PostalAddress( val streetAddress : String, val city : String, val zip : String, val state : USState?, val country : Country ) { assert {(state == null) xor (country == Countries["US"]) } } class PhoneNumber( val country : Country, val areaCode : Int, val number : Long ) object Countries { fun get(id : CountryID) : Country = countryTable[id] private var table : Map<String, Country>? = null private val countryTable : Map<String, Country> get() { if (table == null) { table = HashMap() for (line in TextFile("countries.txt").lines(stripWhiteSpace = true)) { table[line] = Country(line) } } return table } } class Country(val name : String)What's next
Labels:
None

11 Comments
comments.show.hideJul 21, 2011
Anonymous
Why so many curly braces in PostalAddress?
Jul 22, 2011
Maxim Shafirov
Indeed
Jul 22, 2011
Anonymous
I think it should be
(country==Countries["US"])}
Jul 22, 2011
Maxim Shafirov
Thanks!
Jul 23, 2011
Anonymous
I actually found this whole fragment a bit confusing.....can you help clarify?
objectCountries{funget(id:CountryID):Country=countryTable[id]privatevartable:Map<String,Country>?=nullprivatevalcountryTable:Map<String,Country>get(){if(table==null){table=HashMap()for(lineinTextFile("countries.txt").lines(stripWhiteSpace=true)){table[line]=Country(line)}}returntable}
It looks like object is your key word for singleton?
the get function definition expects a CountryID typed object instance be passed in and will then return a Country object by doing a map (get) access of the form countryTable[id]?
the ? at the end of table var means it is nullable?
the table[line] = Country(line) is storing the Country in the table map keyed by string?
But I am confused as to why there is a countryTable and a table? and what is the get() doing? Is that overriding the build in map function get() which is the longhand for the map[x] access? or is the getInstance equivalent initaliser block required by the object singleton?
and where is the CountryID defined? shouldnt that be a string?
Jul 24, 2011
Andrey Breslav
countryTable is a lazy property, backed by table.
get() is a getter for the property
CountryID is supposed to be an enum
And "Yes" to all the other questions
Dec 04, 2011
Anonymous
I think var? would be a better way of specifying nullable type:
privatevar?table:Map<String,Country>=nullinstead of
privatevartable:Map<String,Country>?=nullDec 04, 2011
Andrey Breslav
And how do I describe a list of nullable strings then?
Dec 05, 2011
Anonymous
Judging by your reaction, I must have said something stupid :)
var lst : List<String?>, maybe? :) That would mean that the List itself isn't nullable, and that it contains nullable strings. If you wanted entire list to be nullable, it would be var? lst : List<String>. Does that make any sense?
Dec 05, 2011
Andrey Breslav
Sorry for my first reaction.
The syntax you are proposing looks inconsistent to me. If I want a nullable string I say "var? s : String", but if I want a list of them, I say "var? l : List<String?>" — type syntax is not compositional, and it's strange (C/C++ has been there with arrays and pointers, and the notion of type in these languages is somewhat blurred by this syntax).
Another issue comes from function parameters that don't have var or val infront of them.
Dec 10, 2011
Anonymous
privatevartable:Map<String,Country>?=null
funget(id:CountryID):Country=countryTable[id]
country==Countries["US"]
It looks like type CountryID is missed, and argument of get() should be String, no?
--
Dmitry Mamonov