Skip to content

Inconsistent behavior with converters and named arguments #10900

@ryukoposting

Description

@ryukoposting

It seems like converters are being inconsistently applied to proc arguments.

Example

given the following code:

import options

type
  AllTypesInModule =
    bool | string | seq[int]

converter toOptional[T: AllTypesInModule](x: T): Option[T] =
  some(x)

proc foo(
    a: Option[bool] = none[bool](),
    b: Option[string] = none[string](),
    c: Option[seq[int]] = none[seq[int]]()) =
  discard

This is valid code, no error is thrown:

foo(a = true)
foo(true)
foo(b = "asdf")
foo(c = @[1, 2, 3])

However, any combination of 2 or more arguments, named or not, throws a compile-time error along these lines:

# fixme.nim(25, 4) Error: type mismatch: got <a: bool, b: string>
# but expected one of: 
# proc foo(a: Option[bool] = none(); b: Option[string] = none();
#         c: Option[seq[int]] = none())
#   first type mismatch at position: 2
#   required type: Option[system.string]
#   but expression 'b = "asdf"' is of type: string
# 
# expression: foo(a = true, b = "asdf")
foo(
  a = true,
  b = "asdf")

Current Output

# fixme.nim(35, 4) Error: type mismatch: got <bool, string>
# but expected one of: 
# proc foo(a: Option[bool] = none(); b: Option[string] = none();
#         c: Option[seq[int]] = none())
#   first type mismatch at position: 2
#   required type: Option[system.string]
#   but expression '"asdf"' is of type: string
# 
# expression: foo(true, "asdf")
foo(true, "asdf")
# fixme.nim(18, 4) Error: type mismatch: got <a: bool, c: seq[int]>
# but expected one of: 
# proc foo(a: Option[bool] = none(); b: Option[string] = none();
#         c: Option[seq[int]] = none())
#   first type mismatch at position: 2
#   required type: Option[system.string]
#   but expression 'c = @[1, 2, 3]' is of type: seq[int]
# 
# expression: foo(a = true, c = @[1, 2, 3])
foo(
  a = true,
  c = @[1, 2, 3])
# fixme.nim(51, 4) Error: type mismatch: got <b: string, c: seq[int]>
# but expected one of: 
# proc foo(a: Option[bool] = none(); b: Option[string] = none();
#         c: Option[seq[int]] = none())
#   first type mismatch at position: 2
#   required type: Option[system.string]
#   but expression 'c = @[1, 2, 3]' is of type: seq[int]
# 
# expression: foo(b = "asdf", c = @[1, 2, 3])
foo(
  b = "asdf",
  c = @[1, 2, 3])
# fixme.nim(38, 4) Error: type mismatch: got <a: bool, b: string, c: seq[int]>
# but expected one of: 
# proc foo(a: Option[bool] = none(); b: Option[string] = none();
#         c: Option[seq[int]] = none())
#   first type mismatch at position: 2
#   required type: Option[system.string]
#   but expression 'b = "asdf"' is of type: string
# 
# expression: foo(a = true, b = "asdf", c = @[1, 2, 3])
foo(
  a = true,
  b = "asdf",
  c = @[1, 2, 3])

Expected Output

If calls to foo with one argument using the converter is valid, then calling foo with 2 or more arguments using the converter should also be valid.

Additional Information

$ nim -v
Nim Compiler Version 0.19.4 [Linux: amd64]
Compiled at 2019-02-01
Copyright (c) 2006-2018 by Andreas Rumpf

git hash: b6d96cafc8bcad1f3d32f2910b25cd11a93f7751
active boot switches: -d:release

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions