Is it possible to specify the type of a Capability that has been stored as a generic Capability?

30 views Asked by At

I'm trying to store a dictionary of generic Capabilities inside a resource, something like

pub let storedCapabilities: {String: Capability}

But when I attempt to perform any action on one of those capabilities I got a "cannot infer type parameter: T", e.g.:

self.storedCapabilities[tag]!.check()

I have been trying to specify the capability type using run-time types functions such as:

fun CapabilityType(_ type: Type): Type?
fun ReferenceType(authorized: bool, type: Type): Type

But so far I haven't found the way.

Anybody knows if there's a proper of specifying a generic capability's type?

Cheers!

1

There are 1 answers

0
adk992 On

The Capability method check needs a type T in it in order to be used, otherwise it doesn't know what type it is checking for. Two options:

  1. Use cap.check<T>()
  2. Cast the capability into something with a type, then use check as you would normally
let cap = self.storedCapabilities[tag]!
let casted = cap as! Capability<T>
assert(casted.check(), message: "capability failed check")