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.
Related
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.
Is it the right pattern to use Enumeration for holding shared variable values?
I am accepting arguments from the command line - arguments like "mongoUsername", "mongoPassword", "mongoDatabase" etc. - across a lot of different files, and want to remove the possibility of making a mistake while specifying the argument name.
I created an object as follows:
object CommonParams extends Enumeration {
val MONGO_USERNAME = "mongoUsername"
val MONGO_PASSWORD = "mongoPassword"
..
}
When accepting these parameters from the command line, the parameters will be read using CommonParams.MONGO_USERNAME rather than just "mongoUsername". This method works. My question is:
Is this the right way to do what I am trying to do?
I dont think I am using Enumeration correctly. What should I change?
What would I gain by declaring the CommonParams as follows:
.
object CommonParams extends Enumeration {
val MONGO_USERNAME = Value("mongoUsername")
val MONGO_PASSWORD = Value("mongoPassword")
..
}
If I declared CommonParams this way, I would have to use CommonParams.MONGO_USERNAME.toString each time instead of just using CommonParams.MONGO_USERNAME which is more verbose.
I understand that Enumeration can stand for a certain value being a "thing". However, I am holding a value inside an object attribute. What advantages would I get if I used the second way of declaring CommonParams?
In your first version, you should remove extends Enumeration, since you aren't actually using it.
The benefit of the second version is exactly that CommonParams.Values aren't strings, so that if you have e.g. a method accepting CommonParams.Value, you can't accidentally pass an invalid string. And also that you can get methods like CommonParams.values to list all values.
This question already has an answer here:
Typecasting or initialization, which is better in Swift?
(1 answer)
Closed 6 years ago.
When stumbling across casting using "as! or as?", I also noticed that types could also be converted using the desired type inside of parenthesis such as:
let x : Int = 42
var myString = String(x)
This made me curious to ask if converting and casting are the same? However when I tried to do converting in a another example using a reference type, I don't think the compiler allows this or at least it gave me an error, such as:
let sutCast = storyboard.instantiateViewController(withIdentifier: "ItemListViewController") as! ItemListViewController
let sutConvert = ItemListViewController(storyboard.instantiateViewController(withIdentifier: "ItemListViewController"))
Is it safe to say or assume that in Swift, conversions are not allowed for reference types and casting is different from conversion because it depends if an object is a reference type or a value type?
From the documentation:
Type casting is a way to check the type of an instance, or to treat that instance as a different superclass or subclass from somewhere else in its own class hierarchy.
It is a way to convert one type to another but it can also be used for more, such as to check the type etc.
Refer to the documentation for more info.
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 9 years ago.
Improve this question
When learning other languages there is often a difference between a class method and an object method.
I know that, in Perl, the class is weak. Is there also a difference between a class method and an object method?
I know the most often used class method may be the class's new method. In Perl I can call all the methods with the package name, but not the package's object. Why is that?
The perlobj man page is helpful here:
When you call a method, the thing on the left side of the arrow is passed as the first argument to the method. That means when we call Critter->new(), the new() method receives the string "Critter" as its first argument. When we call $fred->speak(), the $fred variable is passed as the first argument to speak().
In other words, Perl doesn't make a sharp distinction between class methods and instance methods. They're differentiated by what gets passed as the first argument to the method, and if some methods don't actually happen to care about what gets passed as the first argument, then you can cheat and call them the "wrong" way.
Perl won't care. It usually doesn't.
#qwrrty's answer is a good explanation for the situation, but from comments I get the impression that even though Perl makes little distinction between object and class methods, #JackXu would like such a distinction.
If you want to make such a distinction, then the solution is to check $_[0] to see if it's an object or a string, and behave appropriately (e.g. throw an exception if an object method is called with a class name as the first parameter).
There are various method signature modules available on CPAN that make this stupidly easy to do, along the lines of:
package Foo;
method xxx (Object $self: Int $x) {
...;
}
Foo->xxx(1); # throws an error because "Foo" is not an object
I'm going to pimp my own solution for this sort of thing - Moops, which not only gives you method signatures but also keywords for class, role, etc. The particular reason for pimping it here is that thanks to its support for "multi methods", you can even create a class method and an object method with the same name as each other!
use Moops;
class Foo
{
multi method xxx (ClassName $class: Int $x) {
say "CLASS METHOD - value $x";
}
multi method xxx (Object $self: Int $x) {
say "OBJECT METHOD - value $x";
}
}
Foo->xxx(1);
my $foo = Foo->new;
$foo->xxx(2);
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.