<div dir="ltr">Hi Vladimir,<div><br></div><div>Thanks for your code, it's actually quite close to what I want :)</div><div><br></div><div>Some comments:</div><div>1) The main downside is that to read back the value, one can't re-use ".m", ".km", etc. and have to use ".inMeters()", etc. Would have been nice to be able read and write using the same suffixes.<br></div><div><br></div><div>2) Works quite well on assigning values, and works as expected with a lot of control of allowed operations, etc. However can make things verbose as you also mentioned when needing to define many different types, but that's a limitation of Swift not supporting this "Typed Numerics" natively rather than your code :)</div><div><br></div><div>3) Requires an extra byte to store the enumeration value internally, so whereas Double is 8 bytes, Distance would be 9 bytes. Sometimes it might be desirable to store the original type the value was assigned with, for example .km, and only convert to appropriate type when reading, whereas other times it's best to convert immediately to the preferred type on assignment which would be .m (or meters) in this case, perhaps use the "default" keyword next to the type?. Also compiler support would be advantageous here also, so that constant expressions can be evaluated at compile time with no overhead, so that if I put... let dist : Distance = <a href="http://125.km">125.km</a>, dist is converted at compile time to 125000.m.</div><div><br></div><div>4) Agree that this "feature" should rather be a type alias, as we dealing with types rather than a structure.</div><div><br></div><div>5) If Swift could support the shortened typed numeric syntax natively, and with the efficiency that can only happen in the compiler itself so that it works as fast and efficiently as normal untyped numbers that would be awesome :)</div><div><br></div><div>6) I think a feature like this could make Swift even more useful in scientific and other critical applications where they deal with many different types of numbers, such as velocity, acceleration, gravity, power, watts, distance, etc. etc.</div><div><br></div><div>Also normal everyday code, for example,</div><div> func circle(radius: Double) ... //Is that radius in pixels, meters, millimetres or something else? But if radius was defined as our fancy Distance (assuming we add pixels (px) too :), then they could call it with:</div><div> circle(radius: 150.px) //or</div><div> circle(radius: <a href="http://100.mm">100.mm</a>) //or</div><div> circle(radius: <a href="http://10.cm">10.cm</a>)</div><div> //etc.</div><div><br></div><div>This would help make the code clearer, and lead to better quality code and hopefully less bugs.</div><div><br></div><div>Regards,</div><div>Nur</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 22, 2016 at 6:07 PM, Vladimir.S <span dir="ltr"><<a href="mailto:svabox@gmail.com" target="_blank">svabox@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The first question is what the meaning of , for example, a multiplication of Distance ? I.e.<br>
let x1 : Distance = 10.m<br>
let x2 : Distance = <a href="http://10.km" rel="noreferrer" target="_blank">10.km</a><br>
let x3 = x1 * x2 // ???<br>
<br>
I.e. as soon as your Distance is Double, you allows all kind of floating point operations on instances of this type. And some such operations has no meaning for Distance. So, in your proposal, you need to somehow control allowed operations.<br>
<br>
And I believe you can get what you want right now with enums(If I'm not missing something):<br>
<br>
enum Distance {<br>
case km(Double)<br>
case m(Double)<br>
case mm(Double)<br>
<br>
func inKilometers() -> Double {<br>
return inMeters() / 1000.0<br>
}<br>
<br>
func inMeters() -> Double {<br>
switch self {<br>
case .km(let value) : return value * 1000.0<br>
case .m(let value) : return value<br>
case .mm(let value) : return value / 1000.0<br>
}<br>
}<br>
<br>
func inMillimeters() -> Double {<br>
return inMeters() * 1000.0<br>
}<br>
<br>
static func +(lhs: Distance, rhs: Distance) -> Distance {<br>
return .m(lhs.inMeters() + rhs.inMeters())<br>
}<br>
<br>
static func -(lhs: Distance, rhs: Distance) -> Distance {<br>
return .m(lhs.inMeters() - rhs.inMeters())<br>
}<br>
<br>
// implement needed operations here..<br>
}<br>
<br>
extension Double {<br>
var km : Distance { return Distance.km(self) }<br>
var m : Distance { return Distance.m(self) }<br>
var mm : Distance { return Distance.mm(self) }<br>
}<br>
<br>
let dist : Distance = <a href="http://10.km" rel="noreferrer" target="_blank">10.km</a> + 5.m + <a href="http://5.mm" rel="noreferrer" target="_blank">5.mm</a><br>
<br>
print(dist.inMeters()) // 10005.005<br>
print(dist.inKilometers()) // 10.005005<br>
<br>
More verbose, but more control. From other point of view, currently you'll need a lot of code for each such type(like Distance).<br>
<br>
But I do think that such ability to create custom domain-specific types based on standard value types is a useful feature which can improve a quality of code and can reduce the number of bugs.<br>
<br>
The syntax to declare such type should be simple and clear, to be able to declare a number of such types without a lot of boilerplate code. Something like this:<br>
<br>
typealias Distance : Double {<br>
var km : Distance { return self * 1000.0 }<br>
var m : Distance { return self }<br>
var mm : Distance { return self / 1000.0 }<br>
inherit [+,-,/]<br>
}<br>
<br>
IMO such type is more a type alias than new structure<span class=""><br>
<br>
On <a href="tel:22.08.2016%2017" value="+12208201617" target="_blank">22.08.2016 17</a>:54, Nur Ismail via swift-evolution wrote:<br>
</span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
Hi,<br>
<br>
I'm new to the list, but have an idea for Typed Numerics.<br>
Basically numeric values (such as Double, Int, etc.) that are strongly<br>
typed to a specific use case, for example Distance, Weight, etc. and cannot<br>
be intermixed with untyped values.<br>
<br>
So if I have (the syntax is made up, but perhaps something like this):<br>
=====<br>
//Distance<br>
struct fixedtype Distance : Double {<br>
var km: …<br>
var m: …<br>
typealias meters: m<br>
var feet: ...<br>
...<br>
}<br>
<br>
//Weight<br>
struct fixedtype Weight : Double {<br>
var kg: …<br>
var g: …<br>
typealias grams : g<br>
var pound: ...<br>
}<br>
<br>
…<br></span>
var weight : Weight = <a href="http://5.kg" rel="noreferrer" target="_blank">5.kg</a> <<a href="http://5.kg/" rel="noreferrer" target="_blank">http://5.kg/</a>> + 5.g + 7.m<span class=""><br>
………………………………………..^ Compiler Error: Can’t add Distance to Weight...<br></span>
var distance: Distance = <a href="http://7.km" rel="noreferrer" target="_blank">7.km</a> <<a href="http://7.km/" rel="noreferrer" target="_blank">http://7.km/</a>> + 12.5.m + 5.0 + 3<span class=""><br>
………………………………………………...^ Compiler Error: can’t add untyped number to Distance...<br>
===<br>
<br>
The main restriction this syntax should do is disallow intermixing of<br>
numeric types (even if they all descend from Double, Int, etc.) and not<br>
allow adding untyped numerics (i.e. those without a type suffix), unless<br>
explicitly asked for in the code.<br>
<br>
Any of these can be converted to it's raw untyped value, for example:<br>
=====<br>
let number : Double = distance.rawValue + 5.0 //This is allowed<br>
distance += number.m //number is converted to m (meters)<br>
=====<br>
<br>
>From the Swift 3 Language guide, we are for example given the following<br>
example:<br>
=====<br>
extension Double {<br>
var km: Double { return self * 1_000.0 }<br>
var m: Double { return self }<br>
var cm: Double { return self / 100.0 }<br>
var mm: Double { return self / 1_000.0 }<br>
var ft: Double { return self / 3.28084 }<br>
}<br>
<br></span>
let aMarathon = <a href="http://42.km" rel="noreferrer" target="_blank">42.km</a> <<a href="http://42.km/" rel="noreferrer" target="_blank">http://42.km/</a>> + 195.m<span class=""><br>
print("A marathon is \(aMarathon) meters long")<br>
// Prints "A marathon is 42195.0 meters long"<br>
=====<br>
<br>
This is quite nice to suffix a conversion method after the value, but if I<br>
had another extension that converts the values to pounds and kilograms,<br>
then one can illegally do this:<br></span>
let aValue = <a href="http://42.km" rel="noreferrer" target="_blank">42.km</a> <<a href="http://42.km/" rel="noreferrer" target="_blank">http://42.km/</a>> + 195.m + 17.pounds + 5.0<span class=""><br>
and then the code would still compile and run, but not work as intended.<br>
<br>
Extra reading, and inspiration for a feature like the above:<br>
Mars Probe Lost Due to Simple Math Error<br>
<a href="http://articles.latimes.com/1999/oct/01/news/mn-17288" rel="noreferrer" target="_blank">http://articles.latimes.com/19<wbr>99/oct/01/news/mn-17288</a><br>
<<a href="http://articles.latimes.com/1999/oct/01/news/mn-17288" rel="noreferrer" target="_blank">http://articles.latimes.com/1<wbr>999/oct/01/news/mn-17288</a>><br>
<br>
"NASA lost its $125-million Mars Climate Orbiter because spacecraft<br>
engineers failed to convert from English to metric measurements when<br>
exchanging vital data before the craft was launched, space agency officials<br>
said Thursday.<br>
<br>
A navigation team at the Jet Propulsion Laboratory used the metric system<br>
of millimeters and meters in its calculations, while Lockheed Martin<br>
Astronautics in Denver, which designed and built the spacecraft, provided<br>
crucial acceleration data in the English system of inches, feet and pounds.<br>
<br>
As a result, JPL engineers mistook acceleration readings measured in<br>
English units of pound-seconds for a metric measure of force called<br>
newton-seconds."<br>
<br>
<br></span><span class="">
______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-evolution</a><br>
<br>
</span></blockquote>
</blockquote></div><br></div>