diff --git a/02-runtime-check/src/main.rs b/02-runtime-check/src/main.rs index 70bfc3d..37e9098 100644 --- a/02-runtime-check/src/main.rs +++ b/02-runtime-check/src/main.rs @@ -10,7 +10,7 @@ struct CredentialManager { impl CredentialManager { pub fn new() -> CredentialManager { CredentialManager { - locked: false, + locked: true, entries: HashMap::new(), } } diff --git a/03-builder-pattern/src/main.rs b/03-builder-pattern/src/main.rs index e6269d9..51cff0d 100644 --- a/03-builder-pattern/src/main.rs +++ b/03-builder-pattern/src/main.rs @@ -41,9 +41,9 @@ impl UnlockedCredentialManager { } fn main() { - let pw = LockedCredentialManager::new(); - let mut pw = pw.unlock("pssst"); - pw.set("foo", "bar"); - let val = pw.get("foo"); - pw.lock(); + let pw_locked = LockedCredentialManager::new(); + let mut pw_unlocked = pw_locked.unlock("pssst"); + pw_unlocked.set("foo", "bar"); + let val = pw_unlocked.get("foo"); + let pw_locked = pw_unlocked.lock(); } diff --git a/04-typestate/src/main.rs b/04-typestate/src/main.rs index fd6cec9..f6c9967 100644 --- a/04-typestate/src/main.rs +++ b/04-typestate/src/main.rs @@ -11,7 +11,7 @@ struct CredentialManager { } impl CredentialManager { - pub fn new() -> CredentialManager { + pub fn new() -> CredentialManager { CredentialManager { entries: HashMap::new(), state: std::marker::PhantomData, @@ -46,9 +46,9 @@ impl CredentialManager { } fn main() { - let pw = CredentialManager::new(); - let mut pw = pw.unlock("pssst"); - pw.set("foo", "bar"); - let val = pw.get("foo"); - pw.lock(); + let pw_locked = CredentialManager::new(); + let mut pw_unlocked = pw_locked.unlock("pssst"); + pw_unlocked.set("foo", "bar"); + let val = pw_unlocked.get("foo"); + let pw_locked = pw_unlocked.lock(); } diff --git a/05-typestate-go-structs/main.go b/05-typestate-go-structs/main.go index 6957835..18bbcbd 100644 --- a/05-typestate-go-structs/main.go +++ b/05-typestate-go-structs/main.go @@ -35,11 +35,9 @@ func (m UnlockedCredentialManager) Lock() LockedCredentialManager { } func main() { - pw := NewCredentialManager() - - unlocked := pw.Unlock("pssst") - unlocked.Set("foo", "bar") - _, _ = unlocked.Get("foo") - - unlocked.Lock() + pwLocked := NewCredentialManager() + pwUnlocked := pwLocked.Unlock("pssst") + pwUnlocked.Set("foo", "bar") + _, _ = pwUnlocked.Get("foo") + pwUnlocked.Lock() } diff --git a/06-typestate-go-interfaces/main.go b/06-typestate-go-interfaces/main.go index 13b5719..06d453c 100644 --- a/06-typestate-go-interfaces/main.go +++ b/06-typestate-go-interfaces/main.go @@ -52,3 +52,11 @@ func (t *credentialManager) Get(key string) (string, error) { } panic("todo") } + +func main() { + pwLocked := NewCredentialManager() + pwUnlocked, _ := pwLocked.Unlock("pssst") + pwUnlocked.Set("foo", "bar") + _, _ = pwUnlocked.Get("foo") + pwUnlocked.Lock() +} diff --git a/07-type-mapping/main.ts b/07-type-mapping/main.ts index b7ec098..90030b5 100644 --- a/07-type-mapping/main.ts +++ b/07-type-mapping/main.ts @@ -22,7 +22,7 @@ type OpArgsMapping = { }; class WebSocketClient { - public op( + public send( op: TOp, args: TArgs, ) { @@ -32,5 +32,5 @@ class WebSocketClient { function main_ws() { const client = new WebSocketClient(); - client.op(OpCode.PING, {}); + client.send(OpCode.PING, {}); } diff --git a/08-type-programming/main.ts b/08-type-programming/main.ts index 97d718f..4ada33a 100644 --- a/08-type-programming/main.ts +++ b/08-type-programming/main.ts @@ -1,3 +1,9 @@ +// This recursively matches path segments and parameters in segments which +// are surrounded by curly braces (i.e. `{foo}`) in these segments. +// The results are joined to a superset of objects with their params +// via the `&`. +// In example, the path /{foo}/{bar} results in the type +// `{ foo: string } & { bar: string }` type ParseUrlParamsRaw = Url extends `${infer Start}/${infer Rest}` ? ParseUrlParamsRaw & ParseUrlParamsRaw : Url extends `{${infer Param}}` @@ -20,4 +26,5 @@ function navigate

(path: P, params: ParseUrlParams

) { function main() { navigate("/hello", {}); navigate("/hello/{name}", { name: "foo" }); + navigate("/book/{author}/{id}", { author: "foo", id: "bar" }); }