Shouldn't this compile?

I created .NET class library using VS2013 with Update 5 and latest Silver release on Win10.

import System.Threading

public class AtomicBoolean {
  private var val: Int64 = 0
  public var value: Bool {
    get {
      return Interlocked.Read(&val) == 1
    }
    set {
      if newValue
        { Interlocked.Exchange(&val, 1) }
      else
        { Interlocked.Exchange(&val, 0) }
    }
  }
}

It throws compile time error(s) in Silver .NET project:
(E207) Only fields or local variables can be passed as “var” or “out” for parameter “1”.

I’m fairly new to Swift/Silver so I could be doing something wrong.

This Oxygene code compiles fine:

type

AtomicBoolean = public class
protected
  FValue : Int64 := 0;
  method get_Value : Boolean;
  method set_Value(aValue : Boolean);
public
   property Value : Boolean read Get_Value write Set_Value;
 end;

implementation

uses
  System.Threading;

method AtomicBoolean.get_Value : Boolean;
begin
  result := Interlocked.Read(var FValue) = 1;
end;

method AtomicBoolean.set_Value(aValue : Boolean);
begin
  if aValue then
    Interlocked.Exchange(var FValue, 1)
  else
    Interlocked.Exchange(var FValue, 0);
end;

except this:

is a property, not a field, in Swift. we’ll have a workaround for this in 9.1.

This still doesn’t compile in 9.2. Any workaround?

I believe we added a @Field attributed you can add to force ot to become a field instead f a property?

@ck?

I tried this:

@Field var _value: Int64 = 0

but it doesn’t compile. According to Swift docs this appears to be the correct syntax. Or is it?

Looks like something this isn’t working right; i logged an issue (bugs://78469: Silver: @Field attrbute to force a var/let to not become a property) and we’ll try and get it fixed soon. sorry about that.

Fixed for fridays build

The syntax will be

__field value: Int64 = 0

with __field being a new keyword that works symmetrical to var.

Nice :smile:

1 Like