Hi y'all!
I'm trying to reuse the type T of a generic concept AnyFoo[T] as a type of a field of another generic Bar[A: AnyFoo], the code looks like this:
type
AnyFoo[T] = concept foo
foo.color is T
TheFoo = object
color: int
Bar[T, AnyFoo] = object of RootObj
foo: AnyFoo
x: T # instead of using T here, it would be nice to reuse the generic type from AnyFoo
var
f: TheFoo
b: Bar[int, TheFoo]
f.color = 10
b.foo = f
echo typeof(b)
echo typeof(b.foo.color)
echo typeof(f)
This code works, but is it possible to get the type T from AnyFoo so I don't have to specify it separately in Bar[T, AnyFoo]? I feel there should be a better way, as this introduces unneeded complexity and room for error.
I also tried it this way:
type
AnyFoo[T] = concept foo
foo.color is T
TheFoo = object
color: int
Bar[T] = object of RootObj
foo: AnyFoo[T]
x: T
var
f: TheFoo
b: Bar[int]
But it does not compile: test.nim(16, 3) Error: invalid type: 'AnyFoo[int]' in this context: 'Bar[system.int]' for var
Any help and ideas are appreciated!
This seems to work
type
AnyFoo[T] = concept foo
foo.color is T
TheFoo = object
color: int
Bar[F] = object of RootObj
foo: F
x: typeof(F.color)
var
f: TheFoo
b: Bar[TheFoo]
f.color = 10
b.foo = f
echo typeof(b)
echo typeof(b.foo.color)
echo typeof(f)