Capable of doing that would be great(hope to see it in some future release) and answer the question in the comment of Matthew's blog(what good is static member extension):

Is Enumerable.CharRange('a', 'z') really better than EnumerableExtensions.CharRange('a', 'z')?

The answer is of course, I can write these 'type class' style generic function.

By on 2/6/2010 6:32 PM ()

Seems to be another case where type class would help. That is if 'a implement fromintegral it would work.

the equivalent in F# I believe would be something like the following :

1
2
3
4
 

let inline foo (value:int) coeff = coeff * (^a : (static member fromIntegral : int -> ^a) value) 

Unfortunately, 'a in this case must be some defined type rather than standard type like float or int as I failed to find a way to extend the builtin type like int/float. Even c# cannot do it for value type I would assume. I also failed to find the C# extension method equivalent in F# to extend builtin type like list or array.

By on 2/4/2010 6:00 PM ()

Hi,
I posted a more detailed answer about static constraints at StackOverflow here:
[link:stackoverflow.com]

Unfortunately, I don't think this will help you - as Gary wrote, his example won't work with standard F# data types and there is no pre-defined function in LanguagePrimitives that would allow you to do this.

In some cases, you could also use NumericAssociations (in Microsoft.FSharp.Math in PowerPack.dll), which is a lookup table that you can use to get basic operations for working with numeric types with runtime type info as the key. For more info, look for example here: [link:cs.hubfs.net] But again, it doesn't look like it has operations powerful enough to express what you want.

By on 2/5/2010 4:12 AM ()

Found a blog post by Mattew Podwysocki about extension methods.

[link:codebetter.com]

Though I don't understand why the following failed:

1
2
3
4
5
6
7
8
9
10
11
12
 

module MyExtensons

type System.Double with
  static member fromIntegral x = float(x)
  
let inline fromIntegral< ^a when ^a: (static member fromIntegral : int -> ^a) > x = 
  (^a : (static member fromIntegral : int -> ^a) x)
  
let x = fromIntegral<System.Double> 1  

yet just saying 'System.Double.fromIntegral 1' does work.

By on 2/6/2010 12:41 PM ()

For now, inline static member constraints can't "see" extension methods. This could be useful, and is a suggestion we'll consider in a future release.

By on 2/6/2010 12:56 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper