[swift-evolution] [Pitch] Hashable types on RawRepresentable enums or a protocol for custom enum-like types

Ben Rimmington me at benrimmington.com
Tue Oct 4 15:47:18 CDT 2016


> On 4 Oct 2016, at 19:16, Joe Groff wrote:
> 
>> On Oct 4, 2016, at 11:07 AM, Adrian Zubarev <adrian.zubarev at devandartist.com> wrote:
>> 
>> Doesn’t this imply more performance cost? Don’t get me wrong but the value here is not fixed and computed all over again which might waste resources if the calculation is complicated. Sure we could build some workarounds here and there, but the codebase won’t get any prettier after that.
> 
> I doubt it. Formally, integer and string literals are also constructor calls, but they're inlined and constant-folded away in most cases. A struct construction is also likely to be optimized. The code pattern I presented is exactly what the compiler automatically generates when you use the sugar syntax.
> 
> -Joe

It can be surprising when enum cases don't behave like `static let` properties:

	Welcome to Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38).
	  1> import Foundation
	  2> enum Test: NSString {
	  3.     case test
	  4. }
	  5> Test.test.rawValue === Test.test.rawValue
	$R0: Bool = false

-- Ben



More information about the swift-evolution mailing list