Comments (2)
This is how ExpressibleByXXXLiteral
(ExpressibleByDictionaryLiteral
, ExpressibleByStringLiteral
, …) works in Swift.
In the first case the compiler sees a variable foo
of type JSON
initialized by a dictionary literal. That doesn‘t typecheck directly, but then the compiler notices that the left-hand type (JSON
) conforms to ExpressibleByDictionaryLiteral
, and so it offers the following initializer:
init(dictionaryLiteral elements: (String, JSON)...)
And that’s how the compiler can create a value of JSON
type from a dictionary. Your second case works the same way, just using the ExpressibleByStringLiteral
protocol and a different initializer:
init(stringLiteral value: String)
(These all are visible in the Initialization.swift file.)
But this machinery only works for literals, not variables, and that’s why your third example won’t compile – it’s not a literal. (If the ExpressibleByXXXLiteral
machinery worked for variables, too, it would be a nightmare I guess, since you could get a lot of non-obvious automatic conversions.)
Does that make sense?
from generic-json-swift.
OK, thanks for the explanation. Makes sense.
from generic-json-swift.
Related Issues (16)
- Publish new version of the pod? HOT 2
- Add support for Swift Package Manager
- Automate release process HOT 1
- Add Linux support
- Switch to a cross-platform framework
- Usage after decoding HOT 6
- Initialisation using `nil` or `NSNull` HOT 5
- Tags Are Not SPM-Compliant HOT 3
- Mutation support HOT 2
- NSNumber Initialization HOT 3
- How to use JSON in Core Data HOT 7
- Provide `Decoder` to turn `JSON` into `Decodable` HOT 2
- Loop through JSON keys?
- Improve querying using dynamic member lookup? HOT 1
- Merging JSON values HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from generic-json-swift.