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

BUG: can't refer to generic type in return type #8551

Closed
timotheecour opened this issue Aug 6, 2018 · 2 comments · Fixed by #24005
Closed

BUG: can't refer to generic type in return type #8551

timotheecour opened this issue Aug 6, 2018 · 2 comments · Fixed by #24005

Comments

@timotheecour
Copy link
Member

timotheecour commented Aug 6, 2018

reduced a bit from PR #8531 where I'm forced to using auto return instead of explicit type

seems related but not same as #8545

# BUG: can't refer to generic type in return type
import macros

macro distinctBase2*(T: typedesc): untyped =
  let typeNode = getTypeImpl(T)
  expectKind(typeNode, nnkBracketExpr)
  if typeNode[0].typeKind != ntyTypeDesc:
    error "expected typeDesc, got " & $typeNode[0]
  var typeSym = typeNode[1]

  typeSym = getTypeImpl(typeSym)

  echo typeSym.treeRepr # Sym "T"

  if typeSym.typeKind != ntyDistinct:
    error "type is not distinct: " & $typeSym.typeKind

  typeSym = typeSym[0]
  typeSym

when defined(case1):
  # ok
  func distinctBase*[T](a: T): auto = distinctBase2(T)(a)
when defined(case2):
  # Error: type is not distinct: ntyGenericParam
  func distinctBase*[T](a: T): distinctBase2(T) = distinctBase2(T)(a)

when isMainModule:
  type T=distinct int
  discard distinctBase(T(0))
@mratsim
Copy link
Collaborator

mratsim commented Aug 4, 2019

Link to RFC nim-lang/RFCs#44

@metagn
Copy link
Collaborator

metagn commented Sep 1, 2023

Early generic evaluation bug again, delegating to type works because it's a macro call which is delayed

import typetraits

type DistinctBase[T] = distinctBase(T)
func distinctBase*[T](a: T): DistinctBase[T] = distinctBase(T)(a)

type T = distinct int
discard distinctBase(T(0))

Tons of issues are related but I can only link #22607 right now

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