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.
Inheritance for Structs
In RemObjects Swift, struct
s can specify an ancestor, allowing a newly declared struct to inherit the fields and methods of its base struct. Unlike classes, structs are not polymorphic, and members cannot be virtual or overriden.
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 toinout
, 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 thedefined()
function for conditional compilation.
Exception Handling
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.
RemObjects Swift originally provided an interim syntax for exception handling in Swift 1.0 that has been being deprecated moving forward, in favor of the new do
/catch
syntax.
Note that this Exception Handling Language Extension we provided for Swift 1.0 has been deprecated as of Elements version 8.2, and will be removed in a future update. Please refer to the Exceptions and Error Handling topic for the new way to deal with exceptions and errors consistently.