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

document default(T) #16942

Merged
merged 1 commit into from
Feb 5, 2021
Merged

document default(T) #16942

merged 1 commit into from
Feb 5, 2021

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Feb 5, 2021

this PR explains the subtle difference between default(T) and var a: T.

this also explains why default is a magic and can't (at least not currently) be implemented via template default*[T](t: typedesc[T]): T = (var v: T; v)

note also that even without the range constraint issues, template default*[T](t: typedesc[T]): T = (var v: T; v) would run into another problem (typedesc at CT) exaplained in #7596, ie would require this:

proc defaultImpl[T]():T=
  var a: T
  return a
template default*[T](t: typedesc[T]): T = defaultImpl[T]()

but I didn't mention this in the doc comment.

I explicitly mentioned "curently, it generates binary 0", which leaves open possibility that future nim may assign default(T) differently, eg via:

@timotheecour timotheecour changed the title document default document default(T) Feb 5, 2021
@juancarlospaco
Copy link
Collaborator

somePointer.default returns ?, someOption.default returns?, nil, error, something else ?.

@Araq Araq merged commit 6dc7ece into nim-lang:devel Feb 5, 2021
@timotheecour timotheecour deleted the pr_doc_default branch February 5, 2021 18:39
@timotheecour
Copy link
Member Author

@juancarlospaco I thought these cases were clear from the docs but feel free to do a followup PR to add an example with 1 or 2 more examples, eg doAssert cstring.default == nil

ardek66 pushed a commit to ardek66/Nim that referenced this pull request Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants