let type binding


(James Campbell) #1

Currently swift lets you to bind a value to let once:

i.e

let value: UIViewController

if (condition)
{
   value = controllerA
}
else
{
  value = controllerB
}

But currently I have to cast the variable within these blocks to access the
property. It would be great if Swift could treat the let value as being the
type of the object I am setting just for this closure/block (Obviously we
can only set it to a value that matches the type above.)

So I could literally go like this:

let value: UIViewController

if (condition)
{
   value = controllerA
   value.controllerAProperty = []
}
else
{
  value = controllerB
  value.controllerBProperty = []
}

···

--
 Wizard
james@supmenow.com
+44 7523 279 698


(Taras Zakharko) #2

This would make the semantics of the language dramatically more complicated. In essence, what you suggest here is scope-dependent typing of variables. While interesting in principle (a static language with compile-type dynamic typing? yes please!), this is subtle but quite a big change to the language that can potentially open a can of worms elsewhere. I believe one should carefully consider the ramifications first — and no, I don’t have any suggestions yet what these ramifications might be.

Cheers,

Taras

···

Begin forwarded message:

From: James Campbell via swift-evolution <swift-evolution@swift.org>
Subject: [swift-evolution] let type binding
Date: 17 December 2015 at 12:59:05 GMT+1
To: swift-evolution <swift-evolution@swift.org>
Reply-To: James Campbell <james@supmenow.com>

Currently swift lets you to bind a value to let once:

i.e

let value: UIViewController

if (condition)
{
   value = controllerA
}
else
{
  value = controllerB
}

But currently I have to cast the variable within these blocks to access the property. It would be great if Swift could treat the let value as being the type of the object I am setting just for this closure/block (Obviously we can only set it to a value that matches the type above.)

So I could literally go like this:

let value: UIViewController

if (condition)
{
   value = controllerA
   value.controllerAProperty = []
}
else
{
  value = controllerB
  value.controllerBProperty = []
}

--
 Wizard
james@supmenow.com <mailto:james@supmenow.com>
+44 7523 279 698
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution