[nim-dev] Re: Concept[T] design question

  • From: mora <morapeter@xxxxxxxxx>
  • To: nim-dev@xxxxxxxxxxxxx
  • Date: Fri, 29 Jul 2016 07:03:52 +0000

Yes, this would be natural and we could support static values. However, there 
is a case, where ``T`` needs to be injected. Based on Andrea's example, imagine 
that you would like to have
```nim
type HasFloat = concept c
  c.gimme() is float
type HasInt = concept c
  c.gimme() is int
type HasString = concept c
  c.gimme() is string
..
```
It is a little bit long to write, we could use generics to make our code 
shorter:
```nim
type Has[T] = concept c
  g.gimme() is T
``` 
I believe that this example should work in examples where we now ``T`` (e.g., 
``proc f(x: HasType[float])``. In your example would ``type T = ...`` override 
any previously injected ``T``? Do they have to be the same?

I cannot comment on const variables as generic parameters. I have seen 
examples, but never tried and I don't know how that is implemented in the 
compiler. Is there a demand for this? (Based on my limited knowledge, a 
possible workaround could be that you require ``c.getMyType() is static[int]`` 
in the Concept's body, so you can have that value in an extra round, but again, 
I never tried anything like that.)

Other related posts: