Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type Foo[T: static] = object doesn't work; type Foo[T] = object is buggy when instantiated with static generic arg #13529

Open
timotheecour opened this issue Feb 28, 2020 · 6 comments

Comments

@timotheecour
Copy link
Member

timotheecour commented Feb 28, 2020

  • type Foo[T: static] = object doesn't work but should; EDIT: it should work like type Foo[T: static type] = object which does work
  • type Foo[T] = object is buggy when instantiated with static generic arg
  • with type Foo[T: static type] = object, var a: Foo[int] should not compile (var b: Foo[3] does and should)
  • type Foo[T] = object (controversial because potential breaking change, eg for array[N,T]) should probably not accept static inputs (eg var foo: Foo[1.2]) but we should have a way to have both, eg: type Foo[T: static|typedec] = object

Example 1: type Foo[T] = object is buggy when instantiated with static generic arg

example from #13433 (comment) /cc @zah

  type Foo[T] = object
    x: T
  var foo: Foo["test"]
  echo foo
  # static: doAssert foo.T == "test"

Current Output

(x: "")

Expected Output

should not compile

Example 2: type Foo[T: static] = object doesn't work

note type Foo[T: static type] = object does work when instantiating with static generic args

  type Foo[T: static] = object
  var a: Foo[2]
  echo a # ok: ()
  echo a.T # BUG: should be 2, but prints int
  var a2: Foo[1.2] # BUG: Error: cannot instantiate Foo

 # these work:
  type Foo[T: static[float]] = object
  var foo: Foo[1.2]
  doAssert $foo == "()"
  doAssert foo.T == 1.2

  # routines also work
  proc fun(a: static) = (const a2 = a)
  fun(1)
  fun(1.2)
  # this also works
  proc fun2[T](a: static[T]) = (const a2 = a)
  fun2(1)
  fun2(1.2)

Example 3

should this compile? or should it require type Foo[T: static] = object (Which currently doesnt' work)
IMO it should not compile, but it may be useful to have a typeclass that encompasses both types and static, eg: [T: static | typedesc]

  type Foo[T] = object
  var foo: Foo[1.2]

Additional Information

The underlying issue here is that in Generic[T], T defaults to any instead of type

@krux02
Copy link
Contributor

krux02 commented Mar 11, 2020

type Foo[T: static] = object doesn't work but should

No. If you think why this should work please elaborate. If you want to pass static integers to types the declaration type is type Foo[T: static[int]] = object

@timotheecour
Copy link
Member Author

timotheecour commented Mar 11, 2020

No. If you think why this should work please elaborate

  • it works for proc fun(a: static) (and also proc fun2[T](a: static[T]) FWIW)
  • it's the clean way to implement things like WrapStatic, see the whole discussion thread starting here make genericParams support static[T] generic params #13433 (comment)
    because everything else works and that's the only way to specify we expect a static value (of a any type):
block:
  type Foo[T] = object # accepts both types and (controversial) statics
  echo Foo[float]
  echo Foo[3] # currently accepted

block:
  type Foo[T: seq] = object # only accepts seq[T] for some T
  echo Foo[seq[int]]

block:
  type Foo[T: array] = object # only accepts array[N,T] for some N,T
  echo Foo[array[3,int]]

block:
  type Foo[T: typedesc] = object # only accept `typedesc[T]` for some T
  echo Foo[typedesc[float]]
  echo Foo[float] # ok too, as expected
  # echo Foo[3.1] # correctly gives CT error

block:
  type Foo[T: static[float]] = object # only accept static[float]
  # echo Foo[float] # correctly gives CT error
  echo Foo[3.1] # ok: Foo[3.1]

block:
  type Foo[T: static] = object # only accept static[T] for some T
  # echo Foo[float] # correctly gives CT error
  echo Foo[3.1] # BUG (this issue)

If you want to pass static integers to types the declaration type is type Foo[T: static[int]] = object

that's not generic, I need to accept any static[T] where T is a type, so that:
Foo[3] and Foo[3,1] work

Again, see #13433 (comment)

Araq pushed a commit that referenced this issue Apr 14, 2020
…12713 ; refs #13529 (#13976)

* fix #12864 static params were mutating arg types during sigmatch

* fix test

* fix StaticParam

* also fixes #12713; added test case
@timotheecour
Copy link
Member Author

timotheecour commented Apr 14, 2020

now that #13976 was merged the only things left to fix is:

  • type Foo[T: static] = object could mean: type Foo[T: static type] = object
  • type Foo[T] = object should give CT error when instantiated with static generic arg (instead, use type Foo[T: static type] = object today or type Foo[T: static] = object if that gets implemented as described above)
  • type Foo[T: static type|typedesc] = object should work (haven't tried)

@krux02
Copy link
Contributor

krux02 commented Apr 15, 2020

@timotheecour please update the issue description, or open a new followup issue and close this one.

@timotheecour
Copy link
Member Author

I've updated issue description

@metagn
Copy link
Collaborator

metagn commented Nov 3, 2024

static auto works

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

No branches or pull requests

4 participants