Navigation Menu

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

Can not use nim 2's new default instantiation with any object type with a distinct DateTime field #20695

Closed
PhilippMDoerner opened this issue Oct 29, 2022 · 1 comment · Fixed by #21428
Assignees

Comments

@PhilippMDoerner
Copy link
Contributor

What happened?

Related issue that fixes a more fundamental version of this problem: #20681

This is basically a more specialized version/an edgecase of that very same issue, so I'll copy the initial description:

It is currently impossible to instantiate a distinct DateTime object at compile time. That makes it difficult to make use of nim 2's new feature of #20480 for any object with fields of a custom version of the DateTime type.

My original minimal example:

import std/times

type DjangoDateTime = distinct DateTime

type Default = object
  data: DjangoDateTime = DjangoDateTime(DateTime())

let x = default(Default)
echo x.repr

@ringabout narrowed down the issue more specifically to be

import std/times
import std/macros

type DjangoDateTime = distinct DateTime

block:
  macro foo(x: static DjangoDateTime) =
    discard x

  macro foo2: untyped =
    var x: DjangoDateTime = DjangoDateTime(DateTime())

    result = quote do:
      foo(`x`)

  foo2()

Nim Version

Nim Compiler Version 1.7.3 [Linux: amd64]
Compiled at 2022-10-29
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 534c97e
active boot switches: -d:release

However, I've been told by @ringabout that the issue in his example is more fundamental and replicable in older nim versions as well

Current Standard Output Logs

Attempt 1:
type mismatch: got 'DateTime' for 'DateTime(nanosecond: 0, second: 0, minute: 0, hour: 0, monthdayZero: 0,
         monthZero: 0, year: 0, weekday: dMon, yearday: 0, isDst: false,
         timezone: nil, utcOffset: 0)' but expected 'DjangoDateTime = distinct DateTime'

Attempt 2:
<PROJECT_PATH>/src/playground.nim(16, 7) template/generic instantiation of `foo2` from here
<PROJECT_PATH>/src/playground.nim(13, 14) Error: the field 'nanosecond' is not accessible.

Expected Standard Output Logs

No compiler error

Possible Solution

This problem is absolutely beyond me, no idea.

Additional Information

No response

@ringabout
Copy link
Member

ringabout commented Nov 3, 2022

I got another simpler related cases, I will open a separate issue.

import times

proc foo(x: static DateTime = Datetime()) =
  discard

It fails at 1.6.8

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

Successfully merging a pull request may close this issue.

2 participants