[swift-evolution] Proposal: Stored properties for enums

Al Skipp al_skipp at fastmail.fm
Thu Dec 10 07:53:47 CST 2015

> On 10 Dec 2015, at 12:34, Jonathan Hise Kaldma <info at hisekaldma.com> wrote:
> Not really. It certainly works, but it's hard to follow. Also, this example was imagining some type of editor with blocks on a timeline. So the pos and length should really be properties of the block, not of the data.

Fair enough : )

I’d definitely be keen to maintain the distinction between product types and sum types and this proposal seems to lose that. If pattern matching on product types were introduced, it should solve this particular conundrum. Here’s how it could potentially look in Haskell using pattern matching in the ‘doStuff’ function. Not sure how the pattern matching would look in Swift, but this gives an impression of what’s possible.

data BlockType = Audio AudioData | Video VideoData

data Block = Block { dataType :: BlockType, pos :: Double, len :: Double }

doStuff :: Block -> IO ()
doStuff Block {dataType = (Audio audioData), pos = p, len = l} = … function implementation using audioData, p and l
doStuff Block {dataType = (Video videoData), pos = p, len = l} = … function implementation using videoData, p and l

More information about the swift-evolution mailing list