by melling · 7y ago · 0 comments
I’d argue that with type inference, immutability, and non-null as a default, Swift is an easy language for anyone to be productive quite quickly.
let x = “I’m immutable and not null”
let favorite = [“Java“, “Perl”, “Swift”].shuffled().firstBtw, there is also .first(where:) which accepts a predicate.
@interface Foo : NSObject
{
NSString * name;
NSInteger count;
}
- (NSString)name;
- (void)setName:(NSString *)newName;
- (NSInteger)count;
@end
(This is still legal, of course, but bad practice now that there's `@property` synthesis as well as ivars being declarable in either an extension or the `@implementation` block.)Shuffled may accept arguments. First does not
The runtime cost of first is nearly zero, whereas shuffled is doing significant work (constant versus linear time)
It sort of doesn’t matter because autocomplete won’t let you do the wrong thing.
Although I do believe Swift has "virtual properties" that are really a method under the hood.
The fact that you need to use function call syntax for things like this in other languages seems like it is unnecessarily exposing implementation details to the API because of limitations of the language.
Having "first" as a property feels like exposing implementation details to me. Sure, a array/vector is a contiguous slice of memory filled with items of a certain size. But the vector/array is is basically a pointer to the start of the memory + a size, so "first" is not a inherent part of the data structure, rather a computed/derived property.
When I see a property I think member of a struct, which is not the case here.
(ps: this is all really just hair splitting, it's a minor difference that you would get used to pretty quickly either way)
Genetics are indeed useful but such a can of worms that are not worth it IMO