Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a property as a default parameter value for a method in the same class

Tags:

swift

In a Swift class, I want to use a property as a default parameter value for a method of the same class.

Here is my code :

class animal {
    var niceAnimal:Bool
    var numberOfLegs:Int

    init(numberOfLegs:Int,animalIsNice:Bool) {
        self.numberOfLegs = numberOfLegs
        self.niceAnimal = animalIsNice
    }

    func description(animalIsNice:Bool = niceAnimal,numberOfLegs:Int) {
        // I'll write my code here
    }
}

The problem is that I can't use my niceAnimal property as a default function value, because it triggers me a compile-time error :

'animal.Type' does not have a member named 'niceAnimal'

Am I doing something wrong ? Or is it impossible in Swift ? If that's impossible, do you know why ?

like image 250
Pop Flamingo Avatar asked Nov 23 '14 06:11

Pop Flamingo


People also ask

How do you give a parameter a default value?

To assign a default value to a parameter, you use the following syntax: PARAMETERS p ...... DEFAULT f ...... Default value f can be either a literal or a field name.

What is the default value of parameter method of function?

Description. In JavaScript, function parameters default to undefined . However, it's often useful to set a different default value.

Can you assign the default values to a function parameters?

Default parameter in JavascriptThe default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

Can you set default parameters in Java?

Short answer: No. Fortunately, you can simulate them. Many programming languages like C++ or modern JavaScript have a simple option to call a function without providing values for its arguments.


1 Answers

I don't think you're doing anything wrong.

The language specification only says that a default parameter should come before non-default parameters (p169), and that the default value is defined by an expression (p637).

It does not say what that expression is allowed to reference. It seems like it is not allowed to reference the instance on which you are calling the method, i.e., self, which seems like it would be necessary to reference self.niceAnimal.

As a workaround, you could define the default parameter as an optional with a default value of nil, and then set the actual value with an "if let" that references the member variable in the default case, like so:

 class animal {
     var niceAnimal: Bool
     var numberOfLegs: Int

     init(numberOfLegs: Int, animalIsNice: Bool) {
         self.numberOfLegs = numberOfLegs
         self.niceAnimal = animalIsNice
     }

     func description(numberOfLegs: Int, animalIsNice: Bool? = nil) {
       if let animalIsNice = animalIsNice ?? self.niceAnimal {
         // print
       }
     }
 }
like image 104
algal Avatar answered Sep 30 '22 16:09

algal