Implicit Conversions

Hi all,

I am wondering if implicit conversions have been considered as a feature. They are one of the core features of Scala and, if used correctly, they can be very powerful. For example, I was recently looking into literal conversion to types and I noticed that it's quite hard to add support for say literal to Tensor<Int32> conversion; a main issue being the resulting ambiguity when calling overloaded functions. So, I was thinking that syntax like the following could be used to support implicit conversions in Swift:

@implicitConversion(precedence: ...)
func scalarToTensor<Scalar: TensorFlowScalar>(_ value: Scalar) -> Tensor<Scalar> {

This would also allow for setting a precedence level for conversions to avoid ambiguous resolution errors (which were quite common in Scala 2). It would also go a long way in supporting custom DSLs (for which there seems to be a strong push currently in the Swift community with the function builders proposal).

So, are implicit conversions a feature that has been considered or, if not, that could be considered if a pitch was made for it? Also, given that we decided to go with something like this, how difficult would it be to support it in the compiler?


AFAIU, implicit conversions are explicitly rejected as a language feature. Swift encourages explicit conversions via inits, i.e. init(from: OtherType)

1 Like

I was almost certain this is in the Commonly Rejected Changes document, but it isn't. Perhaps it should be added? :thinking:

1 Like
Terms of Service

Privacy Policy

Cookie Policy