Code Monkey home page Code Monkey logo

Comments (8)

hollance avatar hollance commented on June 11, 2024

I haven't looked into it in detail but AFAICT, you already get an automatic namespace that is named after your project or framework. Nesting types just for the purpose of creating another namespace seems a little extreme.

from swift-style-guide.

 avatar commented on June 11, 2024

I like nested types for namespaces, but I think nested types in general are going to take some experimentation.

IIRC, JavaScript does a lot of nested nonsense in order to face private access, like returning functions that capture variables from other functions to hide their scope. It's a powerful feature, but it still feels kludgy to me when used for such purposes.

I'm hoping we have a better solution for the private thing from Apple (which everyone says we will) and then we see how that ties into this.
--------- Original Message --------- Subject: [swift-style-guide] Nested Types (#12)
From: "Ryan Nystrom" [email protected]
Date: 6/10/14 1:22 pm
To: "raywenderlich/swift-style-guide" [email protected]

Definitely a new feature for us. What are some do's/dont's that we should consider?
I really like the example used in "the book".
struct BlackjackCard { enum Suit: Character { // ... } enum Rank: Int { // ... } let rank: Rank, suit: Suit var description: String { // ... } } I love the idea of using nested "private" types to create container classes. Could we use nested types as a means to namespace library/framework classes?

Reply to this email directly or view it on GitHub.

from swift-style-guide.

rnystrom avatar rnystrom commented on June 11, 2024

@hollance Oh very cool, I hadn't seen this.

from swift-style-guide.

 avatar commented on June 11, 2024

Yeah, I wasn't sure about how it works, either. For example:

min is an existing function

If x and y are Ints,

var min = min(x, y)

won't compile because it thinks you are using the name of the variable in its own declaration. So I figured if the compiler is too dumb to tell what's going on here, then I wasn't sure about things like making our own types.

But if we don't need to nest things to get the behavior we want, then we shouldn't. Nesting should be used for things where it solves a problem more easily than another solution.

--------- Original Message --------- Subject: Re: [swift-style-guide] Nested Types (#12)
From: "Ryan Nystrom" [email protected]
Date: 6/10/14 1:29 pm
To: "raywenderlich/swift-style-guide" [email protected]
Cc: "elephantronic" [email protected]

@hollance Oh very cool, I hadn't seen this.

Reply to this email directly or view it on GitHub.

from swift-style-guide.

hollance avatar hollance commented on June 11, 2024

I just submitted the var min = min(x, y) thing as a bug report. It also happens with your own functions:

func hello() -> Int { return 100 }
var hello = hello()

I don't think this is related to nesting or namespaces in any way.

from swift-style-guide.

ColinEberhardt avatar ColinEberhardt commented on June 11, 2024

Personally I think the example from the 'book' is a terrible one! The concept of a Suit is quite separate from the game of Blackjack, and one that you might want to use in a number of other contexts.

I personally use nested classes (in Java & C#) quite sparingly, I only use them when I am required to create a type purely to support the internal implementation of another type. And in this instance I make the nested type private (again I am assuming we will be able to do this in the future with Swift!)

Microsoft has some good guidance on the subject here: http://msdn.microsoft.com/en-us/library/vstudio/ms229027(v=vs.100).aspx

from swift-style-guide.

rnystrom avatar rnystrom commented on June 11, 2024

That's how I've used nested types in Java too, @ColinEberhardt, almost entirely for implementation/parent-class specific reasons (holders and the like).

I could see the value w/ namespacing though, if you were to use some generic name for a class that "assisted" the parent class? Like Person has a PersonNameFormatter but you could just call it Person.NameFormatter? Thoughts?

from swift-style-guide.

gregheo avatar gregheo commented on June 11, 2024

Has anyone used nested types yet? There is some interesting usage by Apple in the Lister WatchKit sample. But I'll close this with the feeling it's a little too uncommon right now to mention in our style guide, but please file an issue or pull request if you feel strongly otherwise.

from swift-style-guide.

Related Issues (20)

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.