Understanding more complex generic types in Nim coming from Typescript

2024-06-19 Thread Araq
So ... it's just like every other widely used type system.

Understanding more complex generic types in Nim coming from Typescript

2024-06-19 Thread imagio
> Model important things, leave others underspecified so that you can add > logging for example without having to rewrite your program That's one of the reasons I think the zio/effect is really good. It lets you specify your requirements and errors completely at the type level but you don't hav

Understanding more complex generic types in Nim coming from Typescript

2024-06-19 Thread imagio
Yeah that was a bad example, sorry. Too much time in TS. Obviously can't do something like that since the type might be either at runtime. I guess would need something like Rust's [Any](https://doc.rust-lang.org/std/any/index.html) to check/cast to a concrete type at runtime.

Understanding more complex generic types in Nim coming from Typescript

2024-06-19 Thread imagio
> Sure but it still has to prove itself in the real world. What usually happens > is that when your function types are very precise is that everything of the > function's implementation is exposed and so the code can hardly evolve > without breaking the function callers and you get viral ripple

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread alexeypetrushin
TS type system, is one of the best currently available. Sadly, so far Nim type system is inferior to TS (in practice, in theory it may be superior). Problems a) Nim doesn't have union types b) can't do backward type infer, forcing you to write `return none(Option[int])` explicitly instead of `r

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread Alogani
Indeed, in Nim, you could do a lot and still be concise with some simple structures. I think concepts are better understood, used and won't be subject to big refactors if they express core elements. And generally those objects don't need a lot of inheritance or complex generics. Creating a type

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread xigoi
Nim’s type classes are a compile-type construction. The actual type has to be known before running the program, so they cannot be used to implement a procedure whose return type changes dynamically depending on the input.

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread Alogani
To complete with sum types: type ErrorCode = enum HttpError, ValueError type MyEffect[T] = object case isError: bool of true: case errorCode: ErrorCode: of HttpError: pageContent: string else: discard else:

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread Alogani
It is not possible, because Nim is a statically strongly typed language. It is not possible to determine exactly the type of your return value. However, you could do something like this: import std/random randomize() type Parent = ref object of RootRef type Chi

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread xigoi
Nim does not have union types. You can use sum types as a replacement, though it will be more verbose.

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread imagio
Nim does have [Typeclasses](https://nim-lang.org/docs/manual.html#generics-type-classes) and sort-of-ADTs via [object variants](https://nim-lang.org/docs/manual.html#types-object-variants) it's just not possible right now AFAICT to have the compiler infer new anonymous ones. Or I guess to put

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread Alogani
Hello imagio, I don't use Typescript enough to understand the construct you research. But I think the kind of idioms you search could be expressed differently in Nim. Your Effect object, if i am not wrong is returned from your function. You can easily return an Option (std/options) to have an o

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread imagio
That was just a minimal example, the actual stuff in and are much more advanced. Structured concurrency, parallelism, error management, retries, dependency injection, request batching, caching, tracing, scheduling, streaming, STM (software transaction

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread Araq
I doubt it can be done as Nim's `|` operator for types does not do what TS's does, so "Inferring" `MyError1 | MyError2` is downright impossible. On the bright side though, you can simply use Nim's builtin effect system (which is not an effect system but nobody noticed for 10 years, so it's good

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread imagio
I work primarily in Typescript and after reading through the docs a few times I'm still a bit confused about how to use generic types in Nim, especially for data types with multiple generic type parameters. I'm a big fan of and heavy user of effect-ts and thought it wou

Understanding more complex generic types in Nim coming from Typescript

2024-06-18 Thread imagio
To simplify the question I think I'm most curious about how one would model a type with 3 generic parameters and then write combinators that manipulate those generics. Like how flatMap is (self: Effect, fn: (v: A) => Effect) => Effect Run How would you model the Effe