tonyarnold,
@tonyarnold@mastodon.social avatar

Is there a way to declare via a Swift Protocol that a property must be isolated to AnyActor in Swift 5.10?

mattiem,
@mattiem@mastodon.social avatar

@tonyarnold Are you trying to express that it must be isolated to some actor, but you do not care which?

teomatteo89,
@teomatteo89@mastodon.social avatar

@mattiem @tonyarnold Would
protocol SomeProtocol: Actor { } do the job here?

mattiem,
@mattiem@mastodon.social avatar

@teomatteo89 @tonyarnold I think this gets to the root of my question. Are you trying to force conformances to be actor types? And if so, why? Could you make the property async instead?

tonyarnold,
@tonyarnold@mastodon.social avatar

@mattiem @teomatteo89 I had wrapped access to a property that I wanted to protect from data races in an actor, and it conformed to a protocol. Marking the property as nonisolated negated what I was trying to do, but it sounds like marking the protocol as Actor vs AnyActor fixes what I was seeing.

mattiem,
@mattiem@mastodon.social avatar

@tonyarnold @teomatteo89 I bet it does, but if you can make it async, you get all of the same protection with a lot more flexibility. Give it a shot!

tonyarnold,
@tonyarnold@mastodon.social avatar

@mattiem @teomatteo89 I will.

When I'm finished cursing about the disconnect between AppKit and async methods. 😉

rjmccall,
@rjmccall@hachyderm.io avatar

@tonyarnold You want the property to inherit the caller’s isolation or something?

tonyarnold,
@tonyarnold@mastodon.social avatar

@rjmccall yeah - right now, if I declare nothing in the protocol, it seems to require marking the property in my implementation as nonisolated. That's not what I want.

cocoaphony,
@cocoaphony@mastodon.social avatar

@tonyarnold @rjmccall is there a reason for attaching this to a property rather than adding an Actor requirement to the protocol? (The alternate is generally to make things the conformance requires async, which might be the thing you’re running into rather than isolation.)

tonyarnold,
@tonyarnold@mastodon.social avatar

@cocoaphony @rjmccall I did mark the protocol as AnyActor. It still wants nonisolated on the protocol property when I conform an actor to it.

ryanbooker,
@ryanbooker@mastodon.social avatar

@tonyarnold @cocoaphony @rjmccall I just did a test locally, if I mark the protocol Actor it works as expected, marking the protocol as AnyActor gives the nonisolated error.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • DreamBathrooms
  • ngwrru68w68
  • modclub
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • GTA5RPClips
  • provamag3
  • ethstaker
  • normalnudes
  • tester
  • osvaldo12
  • everett
  • cubers
  • tacticalgear
  • anitta
  • megavids
  • Leos
  • cisconetworking
  • JUstTest
  • lostlight
  • All magazines