in

ExpressionBlog.com

Microsoft Expression Studio Community

This Blog

Syndication

Mirrored Blogs

Browse by Tags

All Tags » OOP (RSS)
  • Object Oriented F# - Creating Classes

    In the past couple of posts, I covered extension everything in F#.  This allows me to extend .NET types with such things as extension static and instance methods, properties, properties with indexers, events and so on.  But, let's go back to the beginning and cover object oriented programming with F# from the ground up.  I like to stress that F# is not only a first class functional language, albeit a more impure one than say Haskell, but it also treats imperative and object oriented code as first class citizens as well.  To be able to mix and match for the appropriate programming style makes this a very powerful tool, to be able to use functional aspects with first class citizenship, but as well with imperative and object oriented, well, then the sky is the limit.  With that, let's go over some of the things that make F# a player in this space. Let's get caught up to where we are so far: Part 1 - Extension Everything Part 2 - More Extension Everything Defining Classes As I will point out several times, F# is a pretty flexible language.  With this, you will find that there are several ways of defining classes, whether it be explicit constructors, constructors in the type definition among other things.  Constructed classes in F# must follow the sytanx, with those items in the brackets being option, and the ones with the asterisk may appear zero to many times. type TypeName optional-arguments [   as ident ] =   [   inherit   type   {   as base }   ]   [   let -binding |   let - rec bindings ] *   [   do -statement ] *   [   abstract -binding |   member -binding |   interface -implementation ] *   From the above syntax, I can specify the inheritance, local functions and values (let syntax), verify parameters with the do syntax and member properties, methods and interface implementations.  Defining a simple class is very straightforward.  Consider if I want to implement a person class and override the ToString() to output some meaningful input.  The code for implementing this is compact without a lot of pomp and circumstance such as this. type Person = { FirstName : string ; LastName : string ; Age : int   }   with   override x . ToString ( ) =     sprintf "%s %s is %d years old" x . FirstName x . LastName x . Age        let p1 = { FirstName = "Matthew" ; LastName = "Podwysocki" ; Age = 31   } printfn "%s"   ( p1 . ToString ( ) ) What's nice about this approach is that I can define these simple classes and once they are constructed, they are immutable by default.  Very nice for message construction and other operations.  But, if you want more behaviors inside your defined classes, then it's best to use a more explicit syntax as below. Constructing Classes Using the construction style...
  • Object Oriented F# - More Extension Everything

    In a previous post , I covered a few ways we can do extensions methods, properties, events and so on with F#.  After a few chats, I realized I may have missed a couple of cases that I wanted to cover today.  These two cases are extension operators and extension properties with indexers. With these examples, I'll try best to show you my development style, using my FsTest library from CodePlex to set up my expectations of the extensions using F# syntax.  If you're not familiar with FsTest, I recently updated it to support the F# September 2008 CTP . Extension Properties with Indexers One of the common things we like to have with some of our collections is the ability to retrieve the item by index, using the Item or this property.  With such classes as IEnumerable<'a> among others don't support such a construct, but through the use of extension properties with indexers, they can.  Let's set up how our test should be. #light open Xunit open FsxUnit . Syntax open ExtensionFSharp . CollectionExtensions [< Fact >] let items_with_correct_index_should_be_equal ( ) =   let items = { 0   . .   2   . .   100 }      let actual = items . [ 3 ]   let expected = 6   expected | > should equal actual   What I'm going to do is create a collection of 0 to 100 skipping every two numbers.  I will set my expectation that the third item should be six, which should be easy enough to test.  Running this test of course will not work as the extension property isn't defined yet.  Now, let's go ahead and define what that might look like. #light namespace ExtensionFSharp module CollectionExtensions =   type System . Collections . Generic . IEnumerable with          member this . Item       with get ( index ) =         Seq . nth index this   What I was able to do is open the type definition of IEnumerable<'a> and add the Item property, also called the this property in C#, with a get accessor with an index.  This calls the Seq.nth which is a static function on the Seq module to return the item that I want by index.  Now, I can run my test again and check the result. Just the result we were looking for.  We could go ahead and add additional tests for out of range and inequality, but in the mean time, let's move onto another way of extending F# and the language through operators. Extension Operators One of the more interesting ideas is around extension operators.  This would give us the ability to add additional operators to our classes should we feel that they should support add, subtract and so on.  To me, it's a pretty powerful concept to be able to add such things.  Let's set up our test to see how we think it should look. #light open ExtensionFSharp . DrawingExtensions open Xunit open...
  • Object Oriented F# - Extension Everything

    A post by Jeremy Miller caught my eye this morning in regards to extension methods in Javascript .  While I think that's pretty interesting, I don't think it's a real fair comparison.  Instead, I want to revisit C# and even F# with regards to extension methods, because there are a few things I wanted to point out.  This is the start of a series covering object oriented programming techniques and how they are used in F#.  Note that F# is not only a functional language, but it is a general purpose programming language that supports functional, imperative and object oriented techniques.  I hope this series is useful for pointing out that F# fits the need very nicely for object oriented constructs, which is seldom covered. Extension Methods in C# 3.0 With C# 3.0 came the introduction of extension methods which were introduced as part of certain technologies that were LINQ-enabling.  Using these, we could add methods, and only public methods, to any given class we choose.  So, this gave us the ability to do add methods such as ForIndex to an IEnumerable<T> class such as this: static   class Extensions  {      public   static   void ForIndex<T>(       this IEnumerable<T> items,        Action< int , T> action)      {          var index = 0 ;          foreach (var item in items)          {              action(index, item);              index++;          }      }  }  var range = Enumerable.Range( 1 , 10 );  range.ForIndex((i, item) => Console.WriteLine( "{0} - {1}" , i, item));   But overall, I thought this was nice, but didn't go quite far enough.  Why not extension static methods, properties, events, etc?  I'm sure at one point those were considered but somehow dropped along the way, and I think it's a shame quite frankly.     Where should it be?  If this functionality were to exist in C#, how might it look?  Unfortunately, the way it was designed in C# 3.0, it makes it a tad hard to extend for static methods and properties.  For example, how might you declare a get or set if what you're declaring is a method such as this? public   static   bool IsEven( this   int value) {     get { return value % 2 == 0 ; } }   Or if I were to use the property declaration style instead, it seems just as confusing as the above try at this: public   static   bool   this   int IsEven {     get { return   this % 2 == 0 ; } }  ...
Powered by Community Server (Commercial Edition), by Telligent Systems