Implicit conversion between primitive types

Are there are any plans of adding implicit conversion between primitive types?

For example

var float = 5.0

func accept(float: Float) {
    print("this is \(float)")
}

This will raise a warning that double cannot be cast to float. Which is really frustrating since i always have to add type explicitly or cast it to Float.

I would also like to propose to add "f" modifier for floats.
Any thoughts?

Best,
Milos Jakovljevic

Are there are any plans of adding implicit conversion between primitive types?

For example

var float = 5.0

func accept(float: Float) {
   print("this is \(float)")
}

I recently posted a manifesto that covers value subtyping: https://gist.github.com/anandabits/5b7f8e3836387e893e3a1197a4bf144d\. My opinion is that this is the best way to introduce implicit conversions of the kind you’re talking about here.

That said, without changing how Swift handles type inference for unannotated assignments from literals your example would not work with value subtyping. This is a good thing! It is because the type of `float` is actually `Double` which can represent many values that are not possible to represent with a value of type `Float`.

In order to allow this to work the way you intuitively want we would need `float` to be assigned some kind of `***Literal` type that keeps track of the fact that the value is small enough to be represented by `Float`. This `***Literal` type would be a subtype of `Float`.

···

On Feb 18, 2017, at 7:23 AM, Milos Jakovljevic via swift-evolution <swift-evolution@swift.org> wrote:

This will raise a warning that double cannot be cast to float. Which is really frustrating since i always have to add type explicitly or cast it to Float.

I would also like to propose to add "f" modifier for floats.
Any thoughts?

Best,
Milos Jakovljevic
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

The idea of "f for Float" has been raised here. Core team members have said
that it's been explicitly avoided in Swift on purpose, because in their
judgment it has low readability. Instead of "5.0f" you write "5 as Float".

(Unless I'm mistaken, you can also change the default floating point type
to Float in your own code.)

As to promotion for primitive values, it's something that the core team has
said they're aware of and requires design. However, as Matthew said, Float
could be promoted to Double but never vice versa with that feature.

···

On Sat, Feb 18, 2017 at 09:56 Matthew Johnson via swift-evolution < swift-evolution@swift.org> wrote:

On Feb 18, 2017, at 7:23 AM, Milos Jakovljevic via swift-evolution < > swift-evolution@swift.org> wrote:

Are there are any plans of adding implicit conversion between primitive
types?

For example

var float = 5.0

func accept(float: Float) {
   print("this is \(float)")
}

I recently posted a manifesto that covers value subtyping:
https://gist.github.com/anandabits/5b7f8e3836387e893e3a1197a4bf144d\. My
opinion is that this is the best way to introduce implicit conversions of
the kind you’re talking about here.

That said, without changing how Swift handles type inference for
unannotated assignments from literals your example would not work with
value subtyping. This is a good thing! It is because the type of `float`
is actually `Double` which can represent many values that are not possible
to represent with a value of type `Float`.

In order to allow this to work the way you intuitively want we would need
`float` to be assigned some kind of `***Literal` type that keeps track of
the fact that the value is small enough to be represented by `Float`. This
`***Literal` type would be a subtype of `Float`.

This will raise a warning that double cannot be cast to float. Which is
really frustrating since i always have to add type explicitly or cast it to
Float.

I would also like to propose to add "f" modifier for floats.
Any thoughts?

Best,
Milos Jakovljevic
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution