scala : method not getting called without adding private modifier [closed] - scala

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 months ago.
Improve this question
i am new to scala and cant seem to notice any issue here. As mentioned in code comments, if(enableXMLMessageLog(method)) is evaluating to false without executing the method enableXMLMessageLog . Once i add private to this method, it starts to work as expected and returns true on evaluation.
The methods are member of an abstract class.
Any idea what is happening here and why is it only getting called after adding private ?

i think i got it . the method was being called from a unit test class and only the logBookingMessage method was set to when(messageLogger.logBookingMessage(any[LoggingMethod])).thenCallRealMethod() hence it was not able to call the method enableXMLMessageLog as it was being mocked and thus returning false.
On adding private to the method, mocking framework does not mock and call the actual implementation of the method hence returning true.

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();
}

Flutter named constructor and private constructor [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 2 years ago.
Improve this question
What is a named constructor, the private constructor in Flutter?
in what scenario these are used in Flutter?
The main idea behind having named constructors in Dart is so that you can define multiple constructors for one class. You can only define one unnamed constructor, which is used like this:
Foo(...);
But what if you want alternative ways of instantiating Foo? A named constructor lets you do this:
Foo.bar(...);
Foo.baz(...);
Let's use the Border class in Flutter as an example. Border provides four constructors (one unnamed and three named).
Border // unnamed
Border.all // named
Border.fromBorderSide // named
Border.symmetric // named
Each constructor provides an easier way to customize the Border. For example, the Border.all constructor applies the one set arguments to the entire set of borders of another widget (left border, top border, right border, and bottom border). So, in the case of the Border class, these constructors provide a more specific and lightweight way of creating a border.
We can then consider an example of when we might need to use named constructors over the unnamed version. Let's say we have data that represents a news story that we are going to display inside of a widget:
The unnamed constructor could look something like this:
Story({
this.headline,
this.publisher,
this.publicationDateTime,
});
This works for general purposes, but what if you want to also be able to pass it a JSON response from an API? We might want to consider using a named constructor for this so that it can handle both parsed and unparsed data.
Story.fromJSON(this.jsonData);
You can read about factory constructors in Official Documentation and here.
In your case, createInstance is a private constructor that can only be using inside DatabaseHelper class. Named constructors may be used for return created (cached) instances (performance).
Also, Google team created guides for introduction to Dart other platform developers. You can find tutorials like Intro to Dart for Java Developers on this page.

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.

What is the reason you can't use static methods/variables in a class [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 8 years ago.
Improve this question
I was wondering why you cant use the static keyword in a class.
I know that you can use it in a struct but not in a class
Does anyone know why they choose for this?
And if so are there any advantages for letting static away?
It is a terminological bĂȘtise. What Apple did was decide that you use static in an enum or struct and class in a class. In my view this was a very foolish decision, as they are exactly parallel to one another. Many people have posted on the dev forums suggesting that they just call them all static.
The outcome is a candidate for Occam's Razor: two phrases, e.g. static property and class property, or static method and class method, that mean exactly the same thing. And then they try to cover their tracks in the documentation by adding a third term as an umbrella, "type property" (meaning a static-or-class property) or "type method" (meaning a static-or-class method), which just makes the situation even worse.
The badness of the situation is also revealed by what you have to do in protocols: you declare a class property in a protocol and then if a struct adopts it, it calls that a static property. Same thing with a class method in a protocol; an adopting struct must call that a static method. You'd think that this alone would have told them they'd done a bad thing.
Edit: The more I think about it, the more I like the proposal put forth by newacct in a comment below. If Apple had simply used the umbrella keyword type here as part of the language, the whole problem would have been solved. We could have declarations type var, type let, type func, and this would work equally in enums, structs, classes, and protocols across the board.
From the Swift Language Guide on Methods, Apple has chosen to differentiate the syntax used, based upon the fundamental Type that you're building.
Classes declare "Type Methods" with the keyword Class. Example:
class MyClass {
class func myFunc(){ ... }
}
Structs declare "Type Methods" with the keyword Static. Example:
struct MyStruct {
static func myFunc(){ ... }
}
In both cases, crating a Type Method allows you to invoke the method without first creating an instance of the class or struct.
Whereas a non class/static function would require something like
let instance = MyClass()
instance.myFunc()
... declaring the variable as a Type Method allows something like
MyClass.myFunc()

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.