Last active
August 29, 2015 14:04
-
-
Save klaaspieter/f81dea7642c661935dd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let json = [{ | |
name: "Klaas Pieter Annema", | |
age: 28 | |
}, // Compiler infers Integer type for age | |
{ | |
name: "Chris Eidhof", | |
age: "Unknown" | |
} // Compiler infers String type for age | |
]; | |
struct Person { | |
// No types | |
var name | |
var age | |
init(untyped_dictionary) { | |
self.name = untyped_dictionary["name"] | |
self.age = untyped_dictionary["age"] | |
} | |
} | |
// Some magical parsing code | |
untyped_array = parse(json); | |
let person1 = new Person(untyped_array[0]) | |
let person2 = new Person(untyped_array[1]) | |
person1.age // type is Integer | |
person2.age // type is String? (question mark) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment