This file contains hidden or 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
interface Animal { | |
length: number; | |
eat: (food: string) => void; | |
} | |
// Fish extends the Animal interface | |
interface Fish extends Animal { | |
swim: (howLong: number) => void; | |
} |
This file contains hidden or 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
from abc import ABC, abstractmethod | |
class Animal(ABC): | |
@property | |
@abstractmethod | |
def length(self) -> float: | |
... | |
@abstractmethod |
This file contains hidden or 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
from typing import Protocol | |
class Animal(Protocol): | |
length: float | |
def eat(self, food: str) -> None: | |
... | |
# Fish extends the protocol Animal and is itself a protocol |
This file contains hidden or 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
function variadic<T>(...args: T[]): T[] { | |
return args; | |
} | |
function multiGeneric<A, B>(argA: A, argB: B): [A, B] { | |
return [argA, argB]; | |
} |
This file contains hidden or 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
from typing import TypeVar, Sequence, Tuple | |
T = TypeVar("T") | |
def variadic(*args: T) -> Sequence[T]: | |
return args | |
A = TypeVar("A") | |
B = TypeVar("B") | |
def multi_generic(arg_a: A, arg_b: B) -> Tuple[A, B]: | |
return (arg_a, arg_b) |
This file contains hidden or 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
interface FileHandler { | |
open: () => string; | |
close: () => void; | |
} | |
// with usage like | |
function getContents(fileHandler: FileHandler): string { | |
try { | |
return fileHandler.open(); |
This file contains hidden or 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
from abc import ABC, abstractmethod | |
class FileHandlerABC(ABC): | |
@abstractmethod | |
def open(self) -> bytes: | |
... | |
@abstractmethod | |
def close(self) -> None: |
This file contains hidden or 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
from typing import Protocol, runtime_checkable | |
@runtime_checkable | |
class FileHandlerProtocol(Protocol): | |
def open(self) -> bytes: | |
... | |
def close(self) -> None: | |
... |
This file contains hidden or 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
from typing import Protocol | |
from dataclasses import dataclass | |
class HasAge(Protocol): | |
age: int | |
class HasName(Protocol): | |
name: str |
This file contains hidden or 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
from typing import Protocol, Sequence | |
from dataclasses import dataclass | |
class PersonProtocol(Protocol): | |
name: str | |
age: int | |
height: float | |
friends: Sequence["PersonProtocol"] |