swift - conflict with previous value in same-name Shadowing -


i'm reading erica sadun's swift style book , playing around code.

she suggests:

prefer using same-name shadowing in conditional bind. “this approach both conventional , safe:

guard​ ​let​ x = x { ... } ​// yes​

guard​ ​let​ y = x { ... } ​// no​

deliberate same-name shadowing ensures the unwrapped value’s role follows logical intent established name prior if or guard statement. introducing new name blurs line of semantics , can prove dangerous. consistent same-named shadowing prevents adding unintentional shadow symbols in parent scope.

regardless of being opinion or not:

in code, i'm trying do:

guard let data = data else{...} 

but following error:

definition conflicts previous value

a simplified version of function is:

func process(){   data : anyobject?    if x == nil{ data = m}   else if x > 5{ data = j}   guard let data = data else { return } // definition conflicts previous value   somefunc(data) } 

if place guard statement inside if or else if error go away. that's not want.

is there workaround still able same-name shadow.

edit: tried using backticks , following errors: enter image description here

  • expected pattern
  • braced block of statements unused closure


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -