5 ms·
Having to write these manually seems a bit … boilerplate-y. Why do we need the PersonType enum rather than just depend on the peoples' types? Why do we need fun
by zapzupnz 8y ago
Having to write these manually seems a bit … boilerplate-y. Why do we need the PersonType enum rather than just depend on the peoples' types? Why do we need functions to get spells for a person; why not just access the property directly on wizards? Why would you ever want to return an empty array of spells for a muggle rather than just return nothing?
Was it all just to illustrate the point? Because illustrating with unnecessarily complicated code is … unnecessarily complicated, I'd have thought.
I don't know TS well enough to rewrite this, but here's my attempt is Swift at the sample code, but cleaner. I'm sure this is all possible in TS, based on my few usages of it:
In Swift:
protocol Person {
let name: String
}
protocol Muggle: Person {
let isDursley: Bool
}
protocol Wizard: Person {
let spells: [Spell]
}
protocol Slytherin: Wizard {
let darkArts: [DarkArt]
}
protocol Ravenclaw: Wizard {
let extraKnowledge: [KnowledgeItem]
}
protocol Hufflepuff: Wizard {
let badgerFriends: [Badger]
}
protocol Gryffindor: Wizard {
let loyalTo: [PersonName]
}
let people = [
Gryffindor(name: "Hermione Granger", spells: ["Petrificus Totalus"], loyalTo: ["Ron Weasley"]),
Gryffindor(name: "Ron Weasley", spells: ["Wingardium Leviosa"], loyalTo: ["Hermione Granger", "Harry Potter"]),
Gryffindor(name: "Harry Potter", spells: ["Expelliarmus", "Expecto Patronum", "Imperio"], loyalTo: ["Albus Dumbledore"]),
Slytherin(name: "Draco Malfoy", spells: ["Serpensortia"], darkArts: ["Cruciatus", "Avada Kedavra"]),
Hufflepuff(name: "Cedric Diggory", spells: ["Transfiguration"], badgerFriends: ["Wayne"]),
Ravenclaw(name: "Luna Lovegood", spells: ["Expecto Patronum"], extraKnowledge: []),
Muggle(name: "Dudley Dursley", isDursley: true),
Muggle(name: "Prime Minister", isDursley: false)
]
let slytherins = people.filter { $0 is Slytherin } // count is 1
let ravenclaws = people.filter { $0 is Ravenclaw } // count is 1
let hufflepuff = people.filter { $0 is Hufflepuff } // count is 1
let gryffindors = people.filter { $0 is Gryffindor } // count is 3
let muggleNames = people.filter { $0 is Muggle }.map { $0.name }
let extraInformation: [[Any]] = people.map { person in
if let slytherin = person as? Slytherin {
return slytherin.darkArts
} else if let ravenclaw = person as? Ravenclaw {
return ravenclaw.extraKnowledge
} else if let hufflepuff = person as? Hufflepuff {
return hufflepuff.badgerFriends
} else if let gryffindor = person as? Gryffindor {
return gryffindor.loyalTo
} else if let muggle = person as? Muggle {
return muggle.isDursley
}
}
Didn't have to write any of those accessors, mutators, or futz around with any more type checking than absolutely necessary. I'm sure basically the same is possible in TypeScript, too.
- nothrabannosir 8y agoBecause of type erasure. The compiler removes all type information, and these checks are actually to support a common JavaScript paradigm (doing manual, run time checks). Often found in code checking function argument types to determine which version of a “polymorphic” function was called, e.g. jQuery’s $.post. TypeScript is all about annotating existing JavaScript code, so they strive to support the full JavaScript vernacular. http://api.jquery.com/jQuery.post/ http://api.jquery.com/jQuery.post/
- zapzupnz 8y agoAh, so TS types are basically only available at compile time? Does that make the "standard" way to do things to have both TypeScript types AND some alternative form of type information in object prototypes/classes for runtime use? That seems a bit redundant to my ignorant eyes, complicating what was already a complicated dance.
- patates 8y agoYou cannot deal with Javascript without a "complicated dance". I like JS, and TS even more, but facts are facts.
- zapzupnz 8y agoI wonder if that's one of the things that makes wasm appealing, being able to write for the web without having to learn the awkward dance.
- patates 8y agoI think it is. But I also think a significant amount of that awkwardness comes from the DOM and all the backward-compatible cruft that comes with it. I'm ready to be positively surprised but I'm not sure how much of those stuff you can abstract away.
- 8y ago