1

I am trying a bit of type (aka class) method but am confused on the real world application of such methods. e.g. In the following code from tutorialspoint.com -

class Math
{
    class func abs(number: Int) -> Int
    {
        if number < 0
        {
            return (-number)
        }
        else
        {
            return number
        }
    }
}

let no = Math.abs(-35)
println(no)

So my question is that what is happening here when I am writing a type method. At what point of my programming may I need this. Can any one explain with a bit clear and simple example.

8
  • i think you have your terminology a bit mixed up... do you mean class method? Commented Nov 30, 2015 at 7:57
  • 1
    what's a "type method"? and what does a declaration have to do with… what kind of "real time", even? Commented Nov 30, 2015 at 7:57
  • @Fonix No I mean Type Methods. You can fid out in this link that I am refering to - tutorialspoint.com/swift/swift_methods.htm Commented Nov 30, 2015 at 7:58
  • 1
    @Abhisek that site is shit. Seriously. It's a notoriously low-quality resource. don't refer to it as authoritative source of terminology – better yet, stop learning from it. Apple's official documentation ("the Swift book") is perhaps the best learning material you can read on the subject. Commented Nov 30, 2015 at 8:05
  • 2
    @TheParamagneticCroissant the tutorial is actually correct, they call it that in the apple docs Commented Nov 30, 2015 at 8:06

2 Answers 2

3

these kinds of functions are useful when you dont actually need an instance of the type to be made to be able to call it, eg helper methods. take the example you posted, if you call the abs function, you dont really need to make a Math object instantiated to do that (you could be seems unnecessary).

if your abs function wasnt a type method, you would have to go like this

var mathObject = Math()
mathObject.abs(-35)

as apposed to the way you have it in you example

Math.abs(-35)

both statements achieve the same goal, but the 2nd is more elegant (and memory efficient).

there are other reasons as well for using type methods, but this is just the simplest example of one (look up what a singleton is, for another example)

Sign up to request clarification or add additional context in comments.

5 Comments

i like your answer, so i up-voted it, with some notes. Type method is static. You are not able to override it in subclass, like class method. It has a sense also to define type the method inside a class, not just inside the value type. Yes, it is very new for us to hear about type method, but we know the difference between static and class keyword. So, type method has sense for refrence types and for value types as well.
In this case, of course, a global function would be more appropiate instead of a random unneccessary class.
@user3441734 agreed, bit more in-depth than the original question was asking for though, but doesnt hurt to know the terminology better.
@Sulthan agreed, obviously this is simplistic example, but if there were more methods relating to math-like functions, then you probably would want to group them in a class for code neatness
@Fonix Yes, a module would be the best option although it's a bit difficult to create multiple modules inside one workplace. Instead of a utility class, an extension for the Int type would still be much better. Utility classes are not bad in static languages but in languages like Swift there are better ways to achieve the same.
-1
class C {
    class func foo(){}
    // Type method is always static !!!
    static func boo() {}
}

class D: C {
    override class func foo() {}
    // this is not possible for 'Type method'
    override static func boo() {} // error !!!!
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.