Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Add compile time safety for all types #59

Merged
merged 1 commit into from
Jul 12, 2016
Merged

Add compile time safety for all types #59

merged 1 commit into from
Jul 12, 2016

Conversation

keith
Copy link
Contributor

@keith keith commented Jul 11, 2016

Previously Mapper worked by conditionally downcasting values to the
expected types. For example:

let string: String? = map.optionalFrom("field")

Would basically translate to:

JSON["field"] as? String?

This worked perfectly for many common types, such as String and Int.
But this became a problem when you used types that could not be
converted like this such as NSDate. Previously this line of code would
compile without any additions to Mapper:

let date: NSDate? = map.optionalFrom("field")

But since this would translate to:

JSON["field"] as? NSDate?

This would fail 100% of the time. But since it compiled it would leave
callers assuming that it could work. This is no longer the case. Now if
you attempt to cast to a type that doesn't conform to Convertible
(unless it's RawRepresentable or you're using a transformation) it
will fail to compile.

There are some adverse side effects of this change. For one, you can no
longer map to AnyObject [AnyObject] or [String: AnyObject]. This
is because if we made AnyObject conform to DefaultConvertible we
would be in the same place we were before with the NSDate example
compiling, even though it shouldn't. The alternative in the latter 2
cases is to use NSArray and NSDictionary respectively. After you map
to those, you could then map them back to the Swift types if it was
necessary. Another possible solution for all 3 cases is to create a
transformation that returns these types using as?. This way you won't
break the compile time safety for all other types, but you can still get
the types using AnyObject back.

@keith keith force-pushed the ks/compile-time branch from c0e017c to 5d5abe2 Compare July 11, 2016 20:54
Previously Mapper worked by conditionally downcasting values to the
expected types. For example:

```
let string: String? = map.optionalFrom("field")
```

Would basically translate to:

```
JSON["field"] as? String?
```

This worked perfectly for many common types, such as `String` and `Int`.
But this became a problem when you used types that could not be
converted like this such as `NSDate`. Previously this line of code would
compile without any additions to Mapper:

```
let date: NSDate? = map.optionalFrom("field")
```

But since this would translate to:

```
JSON["field"] as? NSDate?
```

This would fail 100% of the time. But since it compiled it would leave
callers assuming that it could work. This is no longer the case. Now if
you attempt to cast to a type that doesn't conform to `Convertible`
(unless it's `RawRepresentable` or you're using a transformation) it
will fail to compile.

There are some adverse side effects of this change. For one, you can no
longer map to `AnyObject` `[AnyObject]` or `[String: AnyObject]`. This
is because if we made `AnyObject` conform to `DefaultConvertible` we
would be in the same place we were before with the `NSDate` example
compiling, even though it shouldn't. The alternative in the latter 2
cases is to use `NSArray` and `NSDictionary` respectively. After you map
to those, you could then map them back to the Swift types if it was
necessary. Another possible solution for all 3 cases is to create a
transformation that returns these types using `as?`. This way you won't
break the compile time safety for all other types, but you can still get
the types using `AnyObject` back.
@keith keith force-pushed the ks/compile-time branch from 5d5abe2 to 72fb80e Compare July 11, 2016 20:56
@keith keith merged commit 72fb80e into master Jul 12, 2016
@keith keith deleted the ks/compile-time branch July 12, 2016 17:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant