codex-storage / questionable

Elegant optional types for Nim
Other
114 stars 5 forks source link

Optional chaining does not work inside generic function #5

Closed TheSimpleZ closed 3 years ago

TheSimpleZ commented 3 years ago

Hello!

The following code:

import questionable

proc printObj[T](a: T) =
  let x = [1,2,3].some
  echo x.?len

printObj(1)

Gives me the following error:

Error: type mismatch: got <Option[array[0..2, int]], proc (x: seq[T]): int{.noSideEffect.} | proc (x: HSlice[len.U, len.V]): int{.inline, noSideEffect.} | proc (x: TOpenArray: openArray or varargs): int{.noSideEffect.} | proc (x: set[T]): int{.noSideEffect.} | proc (x: cstring): int{.noSideEffect.} | proc (w: WideCString): int{.noSideEffect, gcsafe, locks: 0.} | proc (x: type array or array): int{.noSideEffect.} | proc (x: string): int{.noSideEffect.}>
but expected one of:
macro `.?`(expression: typed; brackets: untyped{nkBracket}): untyped
  first type mismatch at position: 2
  required type for brackets: untyped
  but expression 'len' is of type: None
macro `.?`(option: typed; bracket: untyped{nkBracketExpr}): untyped
  first type mismatch at position: 2
  required type for bracket: untyped
  but expression 'len' is of type: None
macro `.?`(option: typed; call: untyped{nkCall}): untyped
  first type mismatch at position: 2
  required type for call: untyped
  but expression 'len' is of type: None
macro `.?`(option: typed; dot: untyped{nkDotExpr}): untyped
  first type mismatch at position: 2
  required type for dot: untyped
  but expression 'len' is of type: None
macro `.?`(option: typed; infix: untyped{nkInfix}): untyped
  first type mismatch at position: 2
  required type for infix: untyped
  but expression 'len' is of type: None
template `.?`(option: typed; identifier: untyped{nkIdent}): untyped
  first type mismatch at position: 2
  required type for identifier: untyped
  but expression 'len' is of type: None

expression: x .? len

Am I missing something or is there a bug somewhere? I'd expect the code to print the length of the array.

Compiler version:

Nim Compiler Version 1.4.6 [Windows: amd64]

Questionable version:

questionable@0.7.0
markspanbroek commented 3 years ago

Thanks for reporting this, your code should definitely work. I’ll take a look.

markspanbroek commented 3 years ago

Fixed in commit e8ab26875885767ddbcff45290f5ba9d5e909c48. If you update to version 0.8.0, it should work.