Skip to content

Instantly share code, notes, and snippets.

@gavr123456789
Last active March 30, 2022 20:28
Show Gist options
  • Save gavr123456789/a4c086ee0ee302966a720393bc386445 to your computer and use it in GitHub Desktop.
Save gavr123456789/a4c086ee0ee302966a720393bc386445 to your computer and use it in GitHub Desktop.
// Bad example
interface IRemoteData<T> {
result?: T
error?: string
isLoading: boolean
isLoaded: boolean
}
// Same on OOP
abstract class RemoteDataOOP<T> {
// private constructor(){}
// ??? нет ничего общего
}
class FailOOP<T> extends RemoteDataOOP<T> {
constructor(public error: string) {
super()
}
}
class SuccessOOP<T> extends RemoteDataOOP<T> {
constructor(public result: T) {
super()
}
}
class LoadingOOP<T> extends RemoteDataOOP<T> {
}
class NotAskedOOP<T> extends RemoteDataOOP<T> {
}
class RandomClass{}
function fooOOP(x: RemoteDataOOP<string>) {
if (x instanceof LoadingOOP) {
console.log("loading");
} else if (x instanceof NotAskedOOP) {
console.log("not Asked");
} else if (x instanceof FailOOP) {
console.log("failed with error: ", x.error);
} else if (x instanceof SuccessOOP /*&& typeof x.result === "number"*/) {
console.log("result = : ", x.result);// any!
processResult(x.result)
} else if (x instanceof RandomClass){
}
// switch (x instanceof ) {
// }
}
// ADT
interface NotAsked {
kind: "NotAsked"
sideLength: number
}
interface Loading {
kind: "Loading"
}
interface Success<T> {
kind: "Success"
result: T
}
interface Fail {
kind: "Fail"
error: string
}
type RemoteData<T> = NotAsked | Loading | Success<T> | Fail;
function foo(x: RemoteData<number>) {
switch (x.kind) {
case "NotAsked":
break;
case "Loading": {
break;
}
case "Fail":
console.log(x.error)
break;
case "Success":
console.log(x.result)
processResult(x.result)
break;
}
}
function processResult(result: number) {
}
@gavr123456789
Copy link
Author

gavr123456789 commented Nov 16, 2021

Nim version:

type 
  NodeKind = enum
      NotAsked,         
      Loading,        
      Fail,       
      Success  
  RemoteData = object      
    case kind: NodeKind
    of NotAsked:
      sideLength: int
    of Loading:
      discard
    of Success:
      result: int
    of Fail:
      error: string


proc foo(x: RemoteData) =
  case x.kind:
  of NotAsked:
    echo x.result
  else:
    echo "nothing"
  
foo(RemoteData(kind: NotAsked))

nim.cfg file to forse nim use type narrowing:

--warning[ProveField]:on
--warningAsError:ProveField

@gavr123456789
Copy link
Author

Kotlin

sealed class RemoteData<T> {
    class NotAsked<T>: RemoteData<T>()
    class Loading<T>: RemoteData<T>()
    class Success<T>(val result: T): RemoteData<T>()
    class Fail<T>(val error: String): RemoteData<T>()
    class Sas<T>(val qwe: String): RemoteData<T>()
}

fun foo (x: RemoteData<String>) {
    when (x){
        is RemoteData.NotAsked -> println(x)
        is RemoteData.Fail -> println(x.error)
        is RemoteData.Loading -> TODO()
        is RemoteData.Success -> println(x.result)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment