cadence
cadence copied to clipboard
Throw error if you link to a path already linked
Currently if you link storage to a path and the target is already linked it will overwrite the old link.
I think it should throw an error in this case. It is very easy to overwrite something by accident. Especially if you can create paths dynamically.
suggested solution
throw error if path linked or create a new method that does this
Yes. A simple solution would be to require the user to simply unlink
the Capability and then allow them to link
afterwards. Overwriting without warning is an easy accident to happen.
+1
Hi @bjartek, I had a quick look at this and have a question to help me clarify the problem.
Does the below example correctly reflect the issue, or have I misunderstood problematic case?
let foo = Foo()
account.save(foo, to: /storage/foo)
let bar = Bar()
account.save(bar, to: /storage/bar)
// Link
account.link<&Foo>(/public/foo, target: /storage/foo)
// Link another storage value to the same path in account.
account.link<&Bar>(/public/foo, target: /storage/bar)
That explains it fully. The last line should throw an error IMHO. If you do not unlink it first.
Excellent! Thanks for clarifying.
Currently the second link doesn't overwrite, but rather returns with a nil
. After the last line, if I then try to get the value at the account path /public/foo
, it returns the first value foo
account.getCapability<&Bar>(/public/foo) // returns a capability to 'foo'
Added some test-cases to verify the current behaviour: https://github.com/onflow/cadence/pull/1416
I guess the suggestion is to make it (the failure at second linking) a bit more explicit, by panicing the runtime, rather than returning a nil
?
Yes
Might not be needed if the new Capability Controllers API is approved
The new Capability Controller API has been released