Article ID: 2832106 - View products that this article applies to.
Collapse this tableExpand this table
F# is a multi paradigm programming language. It is a mix of functional programming language and object oriented programming language. It is also part of programming languages on top of .NET platform, and .NET class libraries are mostly object oriented.
Being as part of programming languages on top of .NET, F# has supports for OOP features. Common or fundamental OOP features inheritances, polymorphism, and encapsulations. Inheritances contains nice feature of OOP, class inheritances and implementations of interfaces.
Interfaces in F# are implemented in two ways:
This is the syntax using class types:
This is a sample code of creating interfaces using class types (from F# in Visual Studio 2010 Tutorial template):
The IPeekPoke will be treated as interface and will have the same signature as declaring interface in C# and VB. By default, the access modifier when not specified is public. This interface can also contain other members not just methods, but it can contain properties:
The interface declaration above can also be written to omit interface keyword that ending with end keyword:
The type IConnectionFactory simulates a database connection commonly found in System.Data. It demonstrate an abstract property of ConnectionString with setter (set) and getter (get), ConnectionState that has getter only, and abstract method of Open and Close.
Note that F# clearly defines that each method that has no parameter and returns no parameter has to have signature as unit, returning no parameter as unit.
To create interface using object expressions, the interface are declared within a class. This is the syntax:
This is a sample of interface implemented using object expressions:
The advantages of using the object expression syntax are subtle: the interface does not have to be named and the scope of the interface is not visible from outside. Concrete implementation has to be implemented directly inside the class. But the disadvantage of using this syntax is quite clear; this syntax has no comparable features in C# and VB.
To create types that implements interface, the interface must be declared using class type syntax.
For example, we can create type that implement IPeekPoke interface above:
Now, to call the method of the interface, F# requires the call to be explicit to specify the interface, and this is clearly specified in MSDN Library:
"Interface methods can be called only through the interface, not through any object of the type that implements the interface. Thus, you might have to upcast to the interface type by using the :> operator or the upcast operator in order to call these methods."This is a sample code to call methods of IPeekPoke:
There is an alternative syntax to call interface method. An alternative is to declare a method on the object that upcasts and calls the interface method, as in the following example, with comparison of having an added sample of using upcast of above:
The code of SomeClass2 declaration define member Print that has upcast to IPrintable.Print and also has concrete implementations of Print method.
For more information about interface in F#, please visit:
Community Solutions Content DisclaimerMICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.
Article ID: 2832106 - Last Review: September 25, 2013 - Revision: 5.1
Contact us for more help
Connect with Answer Desk for expert help.