Skip to content

Confusing error messages for constructors #17793

@konsumlamm

Description

@konsumlamm

Example

1

type Test = object
  x: int

echo Test(42)

2

type Test[T] = object
  x: T

echo Test(42)

3

type Test[T] = object
  x: T

echo Test(x: 42)

Current Output

1

Error: type mismatch: got 'int literal(42)' for '42' but expected 'Test = object'

2

Error: type mismatch: got 'int literal(42)' for '42' but expected 'Test = CompositeTypeClass'

3

Error: object constructor needs an object type

Expected Output

1

Error: expected object constructor

2

Error: expected object constructor

3

No error, T should be inferred to int. The same works with procs:

proc test[T](x: T): T = x

echo test(42) # compiles

Additional Information

$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-04-19
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 3b80f0dc8ef2bc9d2a981a7c92a6355dfc72b7ef
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