Matching Any associated Enum to retrieve associated value

Can anyone suggest a good way to pull an associated value that would exist in all enum cases

enum Cell {
    case height(Fresh<Length>?, link: LinkData)
    case length(Fresh<Length>?, link: LinkData)
    case mass(Fresh<Mass>?, link: LinkData)
    
    var signal: Signal<Void> {
        switch self {
        case height(_, linkData):
            return linkData.signal
        }
    }
}

One way to do this is:

enum Cell {
    case height(Fresh<Length>?, link: LinkData)
    case length(Fresh<Length>?, link: LinkData)
    case mass(Fresh<Mass>?, link: LinkData)

    var signal: Signal<Void> {
        switch self {
		  case .height(_, let linkData),
			   .length(_, let linkData),
			   .mass(_, let linkData): 
                 return linkData.signal
        }
    }
}
2 Likes

Or pull it out into a struct:

struct Cell {
  var link: LinkData
  var data: CellData
  
  enum CellData {
    case height(Fresh<Length>?)
    case length(Fresh<Length>?)
    case mass(Fresh<Mass>?)
  }
}
4 Likes

Appreciate the recommendations guys, it looks like the moving data to a struct was the way to go. A couple hours rewrite and we’re all good!

Thanks :blush:

Отправлено с iPhone

2 Likes
Terms of Service

Privacy Policy

Cookie Policy