Giter Club home page Giter Club logo

Comments (2)

zoul avatar zoul commented on June 25, 2024 1

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.

cjmconie avatar cjmconie commented on June 25, 2024

OK, thanks for the explanation. Makes sense.

from generic-json-swift.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.