I'm just curious if Swift is dynamic like php or static, I mean can I generate classes while an application is running?
It is static - very static. The compiler must have all information about all classes and functions at compile time. You can "extend" an existing class (with an extension), but even then you must define completely at compile time what that extension consists of.
Objective-C is dynamic, and since, in real life, you will probably be using Swift in the presence of Cocoa, you can use the Objective-C runtime to inject / swizzle methods in a Swift class that is exposed to Objective-C. You can do this even though you are speaking in the Swift language. But Swift itself is static, and in fact is explicitly designed in order to minimize or eliminate the use of Objective-C-type dynamism.
Swift itself, is statically typed. When used with Cocoa, you get access to the objective-c runtime library which gives you the ability to use dynamic classes, messages and all. This doesn't mean the language itself is dynamically typed. You could do the same with C or any other language that supports a bridge to C by using libobjc.A.dylib.
Generally you never say static language. You could say static type language or dynamic type language, and you could also say strong type language or not.
So java is a static type language as well as strong type language, because compiler has no ability to detect type automatically, so it's static, and type is strongly restricted so it's a strong type language as well.
And javascript is both a dynamic type language and non-strong type language. Because the compiler has ability to detect type during runtime, and type isn't strictly restricted as well.
So based on above examples, you could say as swift allows us to not declare type and leave to the compiler to detect type by it self, so swift is announced as a dynamic type language by Apple official. And it's also a strong type language for you should use type strictly, when even you haven't declare type, if the compiler detect it to be a String, then it isn't any other type.
Hope it's helpful.
Only when you'd like to use objective c library. Swift is inherently static typed. They just give you interface so as you can gradually move to swift.
Related
Premises:
Ideally, the manner in which one programs in a language should try to match the paradigms to which the language ascribes.
In Swift, those paradigms are primarily protocol oriented programming, and secondarily functional programming.
For reasons of compatibility and interoperability, Swift also supports object oriented programming.
Reference types (Classes) are idiomatic of OOP, while value types (structs, enums, primitives) in conjunction with protocols, are idiomatic of POP.
Conclusion: Whenever possible, one should use value types and protocols, and revert to Classes only when it is entirely necessary.
Inquiry: What then, is the role of a Class containing value types? Do these encapsulating classes align with Swift paradigms, or are they holdovers from OOP?
Base Case: Is the following good or bad practice in Swift:
struct Attribute {
let name: String
var value: Int
}
final class AttributeManager {
var attributes: [Attribute] = []
func add(attribute: Attribute) { self.attributes.append(attribute) }
}
Some of the premises you state are on the wrong track.
Swift is multi-paradigm general purpose language. It does not have OOP just because of compatibility and interoperability. Multi-paradigm languages allow developers to choose most suitable coding paradigm for specific job.
Also, some concepts are not strictly related to single paradigms. Value types are not just functional, they form base kind of types in structural languages. Also many OOP languages that are not functional support value types beyond base primitive ones.
What is called Protocol Oriented Programing is just another form and extension of Interface Based Programming - one of core concepts in Object Oriented Programing.
Your question, about whether or not wrapping value types into the class is good or bad practice cannot be answered because it depends on how you are going to use that class.
Classes and structs have slightly different behavior. Sometimes that difference does not matter so you can freely choose between them, sometimes it does and you will have to use the one that fits your needs.
In your code example main difference between having AttributeManager defined as class opposed to struct is sharing changes to its attributes field. If you deal with class instance you can pass it around as parameter, and you can make and preserve changes made to attributes field across code. Doing that with value type variable is more complicated and it would require either using in-out parameter or returning changed struct through function return value. Classes provide more flexibility here. Also using structs is in contradiction with Singleton pattern where you need to have one and only one instance that can mutate its state.
Each programming paradigm and related concepts provide solutions to specific problems, but they also introduce some other problems. When you are choosing one coding paradigm over another you should choose it in context of problem you are trying to solve.
Further read: Swift Is Not Functional
I have a fairly straightforward question about the binding of data types in the Swift programming language. I was surprised not to find any concrete information on the subject through my searches so I believe it would be best to bring my question to here for an in-depth answer:
How are data types bound in Apple's Swift programming language?
Are data types bound to variables and parameters at compile-time?
Are they bound to a run-time or a combination of the two?
Swift binds data types at compile time. If you are programming Swift using Xcode, you can find the type of any variable in your code by Option-clicking on that variable.
Here is an excerpt from
The Swift Programming Language (Swift 3) (emphasis mine):
Swift is a type-safe language. A type safe language encourages you to
be clear about the types of values your code can work with. If part of
your code expects a String, you can’t pass it an Int by mistake.
Because Swift is type safe, it performs type checks when compiling
your code and flags any mismatched types as errors. This enables you
to catch and fix errors as early as possible in the development
process.
Type-checking helps you avoid errors when you’re working with
different types of values. However, this doesn’t mean that you have to
specify the type of every constant and variable that you declare. If
you don’t specify the type of value you need, Swift uses type
inference to work out the appropriate type. Type inference enables a
compiler to deduce the type of a particular expression automatically
when it compiles your code, simply by examining the values you
provide.
Reading Swift language guide I cannot find explicit information whether Swift is statically dispatched (like basic C++, Java, C#) or dynamically dispatched (like Objective-C).
The documentation of language features (like classes, extensions, generics, etc) seems to suggest that it is statically typed, which might be the source of supposed speed improvements. However, Apple stated on the WWDC 2014 keynote that the language uses the same runtime as Objective-C, and is very compatible with Cocoa/Cocoa Touch, which suggest dynamic dispatch.
Describing C++, Java, and C# as statically dispatched is not particularly accurate. All three languages can and often do use dynamic dispatch.
Swift, similarly, can do both. It does differ from ObjC in that it does not always dynamically dispatch. Methods marked #final can be statically dispatched, as can struct and enum methods. I believe non-final methods can be statically dispatched if the runtime type can be proven at compile time (similar to C++ devirtualization), but I'm not certain about the details there.
According to this excerpt from Y Combinator (https://news.ycombinator.com/item?id=7835099):
From a user's point of view, it's basically straight out of the Rust book, all the gravy with also relaxed ownership and syntax.
It has it all [1]: static typing, type inference, explicit mutability, closures, pattern matching, optionals (with own syntax!
also "any"), generics, interfaces, weak ownership, tuples, plus other
nifty things like shorthand syntax, final and explicit override...
It screams "modern!", has all the latest circlejerk features. It even comes with a light-table/bret-victor style playground. But is
still a practical language which looks approachable and
straightforward.
Edit: [1]: well, almost. I don't think I've caught anything about generators, first-class concurrency and parallelism, or tail-call optimization, among others.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Why Objective-C doesn't support method overloading?
I was going through this link which has the answer to my question. But I was not able to understand what the right guy is trying to say. It will be really great if someone can simplify and provide an explanation.
In C++ the compiler has type information and can choose between several methods based on the types. To do the same in Objective C it would have to be done at run-time, because the compiler knows little about object types due to the dynamic nature of the language (i.e. all objects are of type id). While this seems possible, it would be very inefficient in practice.
I think it's a historical artifact. Objective-C is derived from C and Smalltalk, and neither of them support overloading.
If you want overloading, you can use Objective-C++ instead. Just name your sources with the ".mm" extension instead of just ".m".
Just be careful to be sure you know what you are doing if you mix C++ and Objective-C idioms. For example Objective-C exceptions and C++ exceptions are two completely different animals and cannot be used interchangeable.
What he is trying to say is that method overloading is not possible with dynamic typed languages since in dynamic typed languages the information about each of the object is not known until run time. In a statically typed during compile time the overloading can be resolved. You will just create the same functions with same names but the compiler would have enough information to resolve the ambiguity between various calls which it gets. But in dynamic typed languages since the objects are resolved only during run time it is not possible to resolve between the various calls.
In this video from Google IO 2009, the presenter very quickly says that signatures of methods should return concrete types instead of interfaces.
From what I heard in the video, this has something to do with the GWT Java-to-Javascript compiler.
What's the reason behind this choice ?
What does the interface in the method signature do to the compiler ?
What methods can return interfaces instead of concrete types, and which are better off returning concrete instances ?
This has to do with the gwt-compiler, as you say correctly. EDIT: However, as Daniel noted in a comment below, this does not apply to the gwt-compiler in general but only when using GWT-RPC.
If you declare List instead of ArrayList as the return type, the gwt-compiler will include the complete List-hierarchy (i.e. all types implementing List) in your compiled code. If you use ArrayList, the compiler will only need to include the ArrayList hierarchy (i.e. all types implementing ArrayList -- which usually is just ArrayList itself). Using an interface instead of a concrete class you will pay a penalty in terms of compile time and in the size of your generated code (and thus the amount of code each user has to download when running your app).
You were also asking for the reason: If you use the interface (instead of a concrete class) the compiler does not know at compile time which implementations of these interfaces are going to be used. Thus, it includes all possible implementations.
Regarding your last question: all methods CAN be declared to return interface (that is what you ment, right?). However, the above penalty applies.
And by the way: As I understand it, this problem is not restricted to methods. It applies to all type declarations: variables, parameters. Whenever you use an interface to declare something, the compiler will include the complete hierarchy of sub-interfaces and implementing classes. (So obviously if you declare your own interface with only one or two implementing classes then you are not incurring a big penalty. That is how I use interfaces in GWT.)
In short: use concrete classes whenever possible.
(Small suggestion: it would help if you gave the time stamp when you refer to a video.)
This and other performance tips were presented at Google IO 2011 - High-performance GWT.
At about the 7 min point the speak addresses 'RPC Type Explosion':
For some reason I thought the GWT compiler would optimize it away again but it appears I was mistaken.