-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[Feature request] Return by let/const value (Apply parameter constraints to return values) #6793
Labels
Comments
proc foo(s: T): T{`let`, call} =
shallowCopy(result, s)
proc main =
let x = foo("some input of type T")
var s: seq[T] = @[]
for i in 0 ..< 100:
s.add x # shallowCopy here? or full copy? How do you track lifetimes with your idea? If shallow copies are fine, why is |
Indeed, it wouldn't work once it's wrapped in a var container.
For me it is similar to the "let" for ref object: it is shallow immutable.
Right now you can choose to:
- never being able to shoot yourself in the foot (value semantics)
- shoot yourself in the foot if you don't pay attention (ref semantics)
This would be a third option: a protective gear, it doesn't stop everything
but is good enough for common use cases.
In any case I see that as an optimization not as a protection.(And I expect
containers of expensive-to-copy objects will keep a ref to those instead of
a copy).
The fourth option (?) would be write tracking ?
The fifth option would be a borrow-checker.
…On Nov 24, 2017 12:44 PM, "Andreas Rumpf" ***@***.***> wrote:
proc foo(s: T): T{`let`, call} =
shallowCopy(result, s)
proc main =
let x = foo("some input of type T")
var s: seq[T] = @[]
for i in 0 ..< 100:
s.add x
How do you track lifetimes with your idea? If shallow copies are fine, why
is var so bad? I can create a copy without var easily.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#6793 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AVr1jccoQ1YW7VCmAOQHASUPmAGQRRrrks5s5qwWgaJpZM4QptE->
.
|
Does |
Seems to describe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would like have something similar to C++ const return value so that the compiler statically ensure that the result of a proc is only used in a non-mutable context.
Assuming we use the parameter constraints syntax I would like this to not compile:
This way we can have specialization depending on how the result will be used.
In my opinion the big advantage is when working with read-only slices of big objects.
Note: I tried to make that work with term rewriting macro, you can catch:
but not
var y = foo(x)
The text was updated successfully, but these errors were encountered: