Re: Sequence of typeclass

2019-06-25 Thread leorize
Typeclasses in Nim is only used for generics, meaning that they're a 
compile-time constraint, not a runtime one. You'd need an object variant for 
this case.


Re: Sequence of typeclass

2019-06-24 Thread juancarlospaco
[https://nim-lang.org/docs/manual.html#types-varargs](https://nim-lang.org/docs/manual.html#types-varargs)
 See the Example 2. 


Sequence of typeclass

2019-06-24 Thread thefoxandflea
I would like to do something like the following:


type Encodable = int | string
type Encoded = object
  ...

proc encode(i: int): Encoded = ...
proc encode(s: string): Encoded = ...
proc encode(l: seq[Encodable]): Encoded = ...


Run

This seems to compile fine, however I still cannot do something like:


let encoded = encode(@[1, "a"])


Run

because heterogeneous sequences are not supported. In this scenario, shouldn't 
the common typeclass "unify" the int and string resulting in a homogenous 
sequence?

I know I could use a variant type here, but I would prefer to keep the 
interface as simple as possible.

Is there a suggested workaround for this? Maybe a macro is necessary?