Language Extensions

RemObjects Silver adds a few features to the Swift language to make it fit better on all the platforms it supports. We try to keep these extensions to a minimum, and tastefully within the design aesthetics of the Swift language.

Where additional keywords are needed, we follow the C-style language convention of prefixing these with two underscores ("__") to avoid conflict with future changes to the official Swift language.

Iterators

Iterators are a special type of method that provide an easy and comfortable way to implement custom Sequences.

Partial Classes

Partial Classes allow a single class (or struct) to be declared spanning multiple source files.

Static Classes

Static Classes are classes where all members are static. Silver provides a convenient shortcut to mark a class as static instead of having to mark each individual member.

Custom Cast Operators

Cast Operators allow you to implement custom type casting behavior for your own types, bot. for implicit and explicit (as) casts.

Events

Support for .NET-style multi-cast Events is provided to allow Swift code to fully participate in the .NET event system.

Pure Fields

New in Elements 9.3, the __field keyword works symmetrical to var to declare a field inside a class or struct. Unlike var, the field will be implemented as plain low-level field, and not as property. This distinction can be important when, for example, dealing with Reflection on .NET.

Await Statements

The __await keyword is supported on .NET to unwrap asynchronous calls, similar to how await works in Oxygene and C#. See the Await Expressions topic for more details.

Lock Statements

Similar to C# or Oxygene, __lock Statement can be used to ensure thread-safe access to a block of code.

Using Statenents

Also similar to C# or Oxygene, a __using Statement can be used to make sure an object is properly disposed through the IDisposable/Closable interfaces after the code that uses it is finished.

External Functions

Silver introduces the __external keyword to allow the import of external APIs via P/Invokeand JNI.

Mapped Types

RemObjects Silver has full support for Mapped Types, which are inlined types useful to create cross-platform wrappers with zero overhead. While you won't often implement your own mapped types, you will likely use existing ones, for example from the Sugar library.

Aspects & Attributes

Aspects are special attributes that influence how the compiler emits the final executable. Aspects are covered in more detail in their own section, including how to use them and how to create your own.

Class Contracts

Class Contracts allow code to become self-testing, with Pre- and Post-Conditions for methods and type-wide Invariants.

Smaller Extensions

  • Not meant for user code, the ยก (inverted exclamation point) suffix operator can be used to mark a type as being nullable only if it is a reference type – essentially giving it the same Nullability behavior of the type name being used on its own in Oxygene or C#.
  • Silver allows named parameters in attributes, as well as attribute scope prefixes using :.
  • __out can be used similarly to inout, but parameters will be one-directional.
  • Silver supports the same attribute prefixes as Oxygene and C#, as well as a Swift-specific @main: attribute prefix for applying attributes to the entry point.
  • For symmetry with #available, a special #defined() syntax is provided around the defined() function for conditional compilation.

Version Notes

Support for __field is new in Elements 9.3.

Exception Handling (Legacy)

Exception handling is a crucial feature on the .NET and Java/Android platforms (and frankly, also comes in very handy on the Cocoa platform as well). Silver extends the Swift 2.0 error handling syntax to also support for exception handling on all platforms, as covered in more detail in the Exceptions and Error Handling topic.

Note: Silver originally provided an interim syntax for exception handling that has been being deprecated moving forward, in favor of the new do/catch syntax.