Static and non-static methods in scala [duplicate] - scala

This question already has answers here:
What is the rationale behind having companion objects in Scala?
(7 answers)
Closed 7 years ago.
As we know we don't have static methods in scala. If we have to achieve that taste we declare that class as object. But the problem is that when we declare class as object then all methods present in that object becomes static. And in class all methods all non-static. What I want is that to have static as well as non static methods in the same class, Is it possible? Definitely it would be possible but how??????

To do what you are trying do in Scala you create an object and a class of same name. Put static in object and instance members in Class version of it. This object is called a companion object.

Related

Root class of all classes in Swift? [duplicate]

This question already has answers here:
Why is there no universal base class in Swift?
(2 answers)
Closed 5 years ago.
The Apple's developer documentation says:
NSObject is
The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system.
I wonder what is the root class of all classes in Swift in the example below:
class MyClass {
....
}
Swift classes do not inherit from a universal base class. Classes you
define without specifying a superclass automatically become base
classes for you to build upon.
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html
Is that what you were looking for? :)

When to make a class as Singleton in Swift? [duplicate]

This question already has answers here:
Difference between static function and singleton class in swift [closed]
(3 answers)
Closed 6 years ago.
I have made a class that will contain all the utility methods. So, instead of making it as a singleton, I have marked the methods as static and accessing those methods by the ClassName.methodName without the need for instantiation.
Is this approach OK?
Just consider that a singleton is used in order to ensure that only one instance exists for a given class, and that there’s a global access point to that instance.
I believe that having all utility functions marked as static within a class is a good approach since, as you have stated, you will need to use ClassName.methodName in order to use them.
In addition, based on what you want to achieve and the information provided by this link, I would reassert that having a class with static methods is the best alternative.

Scala: get singleton object from class [duplicate]

This question already has answers here:
Get companion object of class by given generic type Scala
(5 answers)
Closed 6 years ago.
Is it possible in Scala to get the singleton instance of an object when I only have the class? Consider
object A {}
def getSingletonInstance[T](x: Class[T]): Option[T] = {
// output the singleton, if x is a singleton class i.e. defined by an object
// is this possible?
}
getSingletonInstance(A.getClass) // --> should return Some[A]
There are many questions on SO discussing different ways of doing this.
One of them I referenced in a comment to your question.
Here is another one, using "official" scala reflection: Get companion object instance with new Scala reflection API
If you don't mind an approach, involving a little "hacking" (as in using some unofficial/undocumented/coincident features rather than an official API), you can do it much easier with something like this:
val clazz = Class.forName(x.getName + "$")
val singleton = clazz.getField("MODULE$").get(clazz)
Note that a companion object of class T does not have to be an instance of T, so the declaration of your getSingletonInstance won't always work.
EDIT I did not realize that you were passing the class of the object itself to your function, not the companion class. In that case, you don't need to append it with dollar sign in the above code, and don't even need the first line at all. You can just do x.getField("MODULE$").get(x)

How to answer "why scala provides `object` keyword"? [duplicate]

This question already has answers here:
Why are singleton objects more object-oriented?
(7 answers)
Closed 8 years ago.
Someone asked me: Why scala provides object keyword?
I answer that with object we can:
create single instance on the language level
create new instance of other types as factories
combine different traits
But he said "These are what object can do, but not why".
What should I answer this question?
(I just read some articles about object, can I answer: Because scala want to support module on language level? )
object is there to avoid static members. Scala was designed as a pure object oriented language, and static just isn't object oriented. static also makes code harder to understand as you mix code that just doesn't belong together conceptually. In general, separating static and instance members is a good idea.
Also object reduces boilerplate by implementing the singleton pattern for you and makes useless types less present.
Just compare:
class SomeGlobalService {
private SomeGlobalService instance = null
private SomeGlobalService() {
<constructor code>
}
public static SomeGlobalService getInstance() {
if (instance == null)
instance = new SomeGlobalService
return instance
}
<members>
}
SomeGlobalService.getInstance().<someMember>
To
object SomeGlobalService {
<constructor code>
<members>
}
SomeGlobalService.<someMember>
In the first example the package is polluted by the useless class SomeGlobalService which you most defenetely will never use directly. In scala the type is less visible as it is SomeGlobalService.type.

Is there a way to deduce the type of Class in which a Class method is being executed, without instantiating an object? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Objective-c: How can I get Class instance in class method
Title pretty much sums it up.
Let's say I have
+ (void) someMethod
{
[NSString stringWithFormat#"Calling Class: %#", ???];
}
I want to be able to fill in ??? with the class that the class belongs to WITHOUT having to instantiate an object.
I promise I have a good reason for wanting to do this.
The name of the class can be found by NSObject's className or if you want the class itself NSObject's class