Optional parameter in case of enumeration in swift

In a fast function, a function may have optional parameters that have default values, such as:

func f(a:Int, b:Int?=nil) {} f(1); f(1,2); 

I would like to do this with related enumerated values. After this message about enter safe URL routes , I would like to have a route that can take an optional parameter, for example:

 enum StopPoint { case Search(query:String, limit:Int?=nil) } 

However, he says that I cannot have a default value for the parameter in the tuple. However, it is possible to have a case such as case Arrivals(stopId:Int) , but in the general case it is impossible to have a tuple with one named parameter.

So, is it possible to have an enumeration with a default parameter, and is the bound value a tuple or not?

+11
enums swift
source share
4 answers

What can you do:

 enum StopPoint { case Search(query: String, limit: Int?) init(query: String, limit: Int? = nil) { self = .Search(query: query, limit: limit) } } let foo = StopPoint(query: "default") // Search("default", nil) let bar = StopPoint(query: "special", limit: 42) // Search("special", Optional(42)) 
+8
source share

I do not think that's possible. This is just a great feature of function s. Nothing that supports a real tuple underneath. Documents for default parameter values :

You can define a default value for any parameter in a function by assigning a value to the parameter after this type of parameter. If a default value is specified, you can omit this parameter when calling the function.

Enumerations simply do not have this capability. You must explicitly use another enum element to include optional additional parameters:

 enum StopPoint { case Search(String) case SearchLimit(String, Int) } 

Enumerations are quite powerful, but not very powerful. You cannot use the default values, you can, for example, not use the type of the variable (maybe even more "restrictions"):

 enum StopPoint { case SearchMult(String...) // causes compile error } 

As for the potentially interesting “why” -question: I don’t know, I'm not so deeply involved in the language architecture.

+5
source share

You may already have considered this and discounted it: you can get closer to the convenience of what you are looking for by creating a static function on an enumeration:

 enum StopPoint { case Search(query:String, limit:Int?) static func createSearch(query:String, limit:Int?=nil) -> StopPoint { return .Search(query: query, limit: limit) } } let myStopPoint = StopPoint.createSearch("my query") 
+3
source share

You can do this by creating static functions for an enumeration that correspond to the enumeration case for which you want to enable additional parameters.

 enum StopPoint { case search(query: String, limit: Int?) static func search(query: String) -> StopPoint { return .search(query: query, limit: nil) } } 

This makes it so that you can provide a constraint or not using the expected syntax:

 let a = StopPoint.search(query: "Foobar") let b = StopPoint.search(query: "Foobar", limit: 5) let c = StopPoint.search(query: "Foobar", limit: nil) func foo(_ stop: StopPoint) { /* ... */ } foo(.search(query: "Foobar")) foo(.search(query: "Foobar", limit: 5)) foo(.search(query: "Foobar", limit: nil)) 

Tested and confirmed work with Swift 4.2

0
source share

All Articles