Why you not use an open keyword with structs in swift? [closed] - swift

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 14 days ago.
This post was edited and submitted for review 14 days ago.
Improve this question
everywhere talk about only classes with open/public keyword so it would be great if swift experts can provide satisfying answers with an example.
for eg: we can do this in class but don't do it in the struct!
open class Animal {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}

open is irrelevant to structs, since they don't have inheritance. public relates to visibility from outside the module, so it is relevant to all types.

Related

Non-nullable instance field _repository must be initialized. Try adding an initializer expression, or add a field initializer in this constructor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
enter image description here
Non-nullable instance field '_repository' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.dartnot_initialized_non_nullable_instance_field in the flutter
In Dart constructors don't use their body to initialize properties. What you are looking for is the following snippet:
class NewsService {
Repository _repository;
NewsService() : _repository = Repository();
}

I want my custom Error type has name "Error" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Not "MyError" or with any other prefixes or suffixes. I want it to be just "Error". Because when I refer this type in the code it would be like: MyApp.Error. Nice and simple.
enum MyApp {
enum SomeModel {}
enum OneMoreModel {}
enum Error: Error {
}
}
It does't compile:
'Error' has a raw type that depends on itself
I tried Error: swift.Error but
Cannot find type 'swift' in scope
What else can I try?
The name of the module is Swift, with a capital "S".
enum Error: Swift.Error {
}

Naming of filenames, classes and protocols? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
When I use protocols with structural patterns such as MVP, or VIPER I usually add protocol and class conforming to the protocol in the same file.
For example, in MVP for the View component:
file: ListViewController.swift
protocol ListView {}
class ListViewController: ListView {}
and for the Presenter component:
file: ListPresenter.swift
protocol ListPresenter {}
class MainListPresenter : ListPresenter {}
1) is adding protocols and classes (when there is only one class conforming to it) in the same file, a good practice?
2) is ok to name the file containing the view controller with its name?
3) can I name the presenter file with the protocol name or do I lack of consistency?
How would you name filenames and classes in this example?
thanks
1) is adding protocols and classes (when there is only one class conforming to it) in the same file, a good practice?
If there's only one class conforming to a protocol, I would strongly argue that you're misusing protocols. If your pattern encourages this, then I would strongly question the value of the pattern.
But putting them in the same file is fine. Placing closely related types in a single file is good.
2) is ok to name the file containing the view controller with its name?
Sure.
3) can I name the presenter file with the protocol name or do I lack of consistency?
Sure...but again, if you have a protocol that has exactly one implementation, then something is likely wrong, and you should be digging into improving that rather than filenames.

In Scala how can I cast a value without asInstanceOf? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have code that returns a value as Any. I have a way to get the actual type of this value. This is part of a very reflection-oriented solution.
// Reflecting on a class I have info about its constructor fields:
val fieldMembers: fieldMembersByName: ListMap[String,ClassFieldMember] = ...
// ClassField member has a method valueIn(inst:T): Any, where T is the type
// of the parent class
val nameField = fieldMembers("name").valueIn(personObject)
// This obtains the value of some instance's (personObject's) name field,
// but to Scala the type of nameField is Any. I need to cast it, but to what?
val realType: Type = ... // a bunch of ugly reflection that returns the acutal
// type of the name field, i.e. String here
What I'm looking for is the equivalent of the (syntactically invalid):
val purifiedValue = nameField.asInstanceOf[realType]
This is not possible for the simple reason that the type of a variable is defined at compile time not at run time.
So when you write
val purifiedValue = nameField.asInstanceOf[realType]
the compiler needs to decide what is the type of purifiedValue. There is no way that it can do this if realType is not defined until run time.
It is also not clear how this would help even if it did work. What are you going to do with purifiedValue once you have assigned it? You can't call any methods on it because you don't know what type it is going to have. You can't pass it to a function because you don't know if the type matches the argument type of the function argument. The only things that you can do with purifiedValue are the things you can do with Any, which is the type it had in the first place.

When do I need to declare class data members as public? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is there any situation when I need to declare the class data members as public?
Are there any chances of errors with such declarations?
Need to? No.
Any data access can be done through getters and setters:
(the truth of the above statement may depend on the language)
private member
public getMember()
return member
public setMember(newValue)
member = newValue
Instead of
public member
Errors? No. Well, apart from having to change your code to use the getters and setters instead.
Want to? Possibly.
The main disadvantage of using getters and setters is how pretty your code looks - how readable it is and code bloating.
See this for advantages of using getters and setters.
If using getters and setters classifies as similar to making the member public for you, then there are many scenario's where you need this at one of these kinds of access. Consider an artificial example of having a Car class. There would be many reasons why you'd want to get the windscreen member variable.