[swift-evolution] Proposal: newtype feature for creating brand new types from existing types

Nicky Gerritsen nickygerritsen at me.com
Fri Dec 11 14:34:16 CST 2015


> On 11 dec. 2015, at 21:30, Gwendal Roué via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Considering the number of times I’ve read about this topic in other languages, I must agree :-)
> 
> Yet there are two difficulties:
> 
> First, what does Bar get from Double ?
> 
> When you say "after creating Bar, I can extend Bar independenly of Double", the problem lies in the meaning of "after". In a language like Ruby, there is an "after", but there is no such clear thing in Swift: modules/packages are compiled as a whole, and module A can extend a type defined the the module B.
> 
> More, let’s see what happens below:
> 
> 1	let d: Double = 1.0
> 2	let b: Bar = d			// Compiler error, I presume.
> 3	let b: Bar = Bar(d)		// Mandatory so that Bar can be put to any use
> 4	let b: Bar = 1.0
> 
> That point 4 is tricky.
> 
> If it is not an error, then 2. should succeed as well, don’t you think?
> If it is an error, then one may want to add FloatLiteralConvertible to Bar. Oops, Double already implements it.
But that is not true. Take this example:

let x: Int = 3 // Type added for clarity
let y: Double = x // error
let z: Double = 3 // ok

This is exactly why it is Float_Literal_Convertible, it can only convert literals ;).

Anyway, about the general thing: isn’t this just inheritance, but then for struct types?
i.e.

struct Bar : Double {}

— Nicky
> 
> Hm. I’m afraid I haven’t found the solution :-)
> 
> Gwendal Roué
> 
>> Le 11 déc. 2015 à 21:09, Tuur Anton via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> a écrit :
>> 
>> //Existing typealias feature.
>> //Foo acts exactly the same way as Double. It's the same thing.
>> //Double can be used instead of Foo anywhere.
>> typealias Foo = Double
>> 
>> //Proposed newtype feature.
>> //Bar acts exactly the same way as Double and  is the same
>> //thing behind the scenes (until we extend it; see below).
>> //Trying to use Double instead of Bar will result in a compile-time error.
>> newtype Bar = Double
>> 
>> Of course, after creating Bar - a "copy" of the type Double, so to speak - I can extend Bar independenly of Double as one would expect.
>> 
>> This would be highly useful.
>> 
>> Your thoughts?
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/8590ebbd/attachment.html>


More information about the swift-evolution mailing list