Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difficulties to assign default value to a parameter of a function

Tags:

swift

swift4

In a class, I defined a private constant, I try to use the constant as a default value of parameter of a function:

class Foo {
  // instance variable
  private let DefaultValue = 10

  // Compiler error: Cannot use instance member 'DefaultValue' as a default parameter
  public func doTask(amount: Int = DefaultValue) {
    ...
  }
}

But I get compiler error: Cannot use instance member 'DefaultValue' as a default parameter.

Then, I also tried declare DefaultValue as private static:

class Foo {
      // static variable
      private static let DefaultValue = 10

      // Compiler error: Static let 'DefaultValue' is private and cannot be referenced from a default argument value
      public func doTask(amount: Int = DefaultValue) {
        ...
      }
    }

But I get new compiler error: Static let 'DefaultValue' is private and cannot be referenced from a default argument value

I need to keep DefaultValue private to this class & I would like to assign default value to the parameter of function with a private variable, whether this is achievable in Swift 4?

like image 342
Leem Avatar asked Sep 18 '18 18:09

Leem


People also ask

Can you assign the default values to a function parameters?

Default parameter in Javascript The 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.

Which parameter Cannot have a default value?

An IN OUT parameter cannot have a default value. An IN OUT actual parameter or argument must be a variable.

Under what circumstance is it impossible to assign a default value to a parameter?

Under what circumstance is it impossible to assign a default value to a parameter while declaring a function? When a parameter is declared as being passed by reference you cannot specify a default value for it, since the interpreter will expect a variable that can be modified from within the function itself.

Can you assign the default values to a function parameters True or false?

It is called default function parameters . It allows formal parameters to be initialized with default values if no value or undefined is passed.


2 Answers

I don't think that is possible. The default value is inserted at the calling site, and therefore needs to be public, see also Access control in swift 4.

A possible workaround would be to make the parameter optional, and substitute nil by the default value locally:

class Foo {
    private static let DefaultValue = 10

    public func doTask(amount: Int? = nil) {
        let amount = amount ?? Foo.DefaultValue
        // ...
    }
}
like image 159
Martin R Avatar answered Oct 23 '22 08:10

Martin R


A somewhat complicated, but workable, solution, for hiding the default value you want within your class is to use a protocol and a conforming struct whose intimate details are known only by the file declaring Foo:

// enabling custom stuff
public protocol IntLike {
    var intValue: Int { get }
}

// allowing to pass Int's
extension Int: IntLike {
    public var intValue: Int { return self }
}

public class Foo {
    // the placeholder
    public struct FooIntLike: IntLike {
        // what we really want to hide
        fileprivate let realInt = 10

        public init() { }

        public var intValue: Int = Int.max // or 0, or whatever
    }

    public func doTask(amount: IntLike = FooIntLike()) {
        // default value will expand to a non-nil value for `realInt`
        let amount = (amount as? FooIntLike)?.realInt ?? amount.intValue
        // do your stuff with the amount
    }
}

Callers of doTask are able to pass Int's, while not knowing what the default value provides.

like image 20
Cristik Avatar answered Oct 23 '22 08:10

Cristik