Skip to content

Can't use generic function for initializing field of generic object #21941

@tsoj

Description

@tsoj

Description

The following snippet doesn't compile:

func what[T](): T =
    discard

type MyObject[T] = object
    f: T = what[T]()

var m: MyObject[float] = MyObject[float]()
echo m

However, this one works fine:

func what[T](): T =
    discard

type MyObject = object
    f: float = what[float]()

var m: MyObject = MyObject()
echo m

Nim Version

Nim Compiler Version 1.9.3 [Linux: amd64]
Compiled at 2023-05-27
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 2beea72
active boot switches: -d:release

Current Output

/tmp/testtest/main.nim(5, 12) Error: cannot instantiate: 'what[T]'

Expected Output

(vec: 0.0)

# or an easier to understand error message

Possible Solution

No response

Additional Information

No response

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