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

=default type bound operator to override default and var initialization #290

Open
timotheecour opened this issue Nov 25, 2020 · 3 comments
Labels

Comments

@timotheecour
Copy link
Member

timotheecour commented Nov 25, 2020

#252 was already accepted in the form of #252 (comment)

This leaves open another point, which is initialization for non-object types (as was suggested in #252 (comment))

proposal

allow type bound =default operator to override default and var initialization.
it must be a compileTime proc (or at least compiler will only call it at CT).

invariant: var a: T is always equivalent to var a = T.default

example 1

# sub.nim:
type Bar* = distinct bool
proc `=default`(t: typedesc[Bar]): Bar {.compileTime.} = true.Bar
  # type bound operator, so doesn't need to be exported

# main.nim
import sub
var a1 = Bar.default
doAssert a1.bool
var a2: Bar
doAssert a2.bool

example 2

in system.nim, add

proc `=default`(t: typedesc[range]): t = t.low

so that:

var a: range[10..20]
static: doAssert a == 10
@timotheecour timotheecour changed the title proc =default(t: typedesc): t {.compileTime.} type bound operator to override default` proc =default(t: typedesc): t {.compileTime.} type bound operator to override default Nov 25, 2020
@timotheecour timotheecour changed the title proc =default(t: typedesc): t {.compileTime.} type bound operator to override default proc =default(t: typedesc): t {.compileTime.} type bound operator to override default and var initialization Nov 25, 2020
@timotheecour timotheecour changed the title proc =default(t: typedesc): t {.compileTime.} type bound operator to override default and var initialization =default type bound operator to override default and var initialization Nov 25, 2020
@cooldome
Copy link
Member

cooldome commented Nov 25, 2020

Why not =init(x: var T) as proposed in #252?. It is more efficient, no extra move/copy operation required

@timotheecour
Copy link
Member Author

why would it be more efficient? =default is {.compileTime.} and affect semantics of var initializaiton, so that:

var a: Bar
  # a is not zero-initialized, it's directly initialized to `=default(Bar)`,
  # possibly from ROM

Copy link

This RFC is stale because it has been open for 1095 days with no activity. Contribute a fix or comment on the issue, or it will be closed in 30 days.

@github-actions github-actions bot added the Stale label Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants