Skip to end of metadata
Go to start of metadata
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
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Jul 21, 2011

    Anonymous

    Why so many curly braces in PostalAddress?

    1. Jul 22, 2011

      Indeed

  2. Jul 22, 2011

    Anonymous

    I think it should be

    (country==Countries["US"])}

    1. Jul 22, 2011

      Thanks!

  3. 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?

    1. Jul 24, 2011

      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

  4. Dec 04, 2011

    Anonymous

    I think var? would be a better way of specifying nullable type:

    private var? table : Map<String, Country> = null

    instead of

    private var table : Map<String,Country>? = null
    1. Dec 04, 2011

      And how do I describe a list of nullable strings then?

      1. 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? 

        1. Dec 05, 2011

          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.

  5. 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