Would anybody please guide me in this?
By now I am using an untyped version (assuming all types are 'System.Object') and some casting.
But It would be nice to have that kind of generics (and maybe we have, and I can not recognize where I am wrong about it).
Thanks

By on 6/2/2007 2:37 AM ()

Try to give the contraint just after the type definition.
Hope this helps.
Can.

1
2
3
4
5
6
7
8
9
let SerializeObject<'T when 'T :(new : unit -> 'T) > (objectToSerialize : 'T) : string =
  let result = new System.Text.StringBuilder()
  let sw = new System.IO.StringWriter(result)
 try  let xs = new System.Xml.Serialization.XmlSerializer ((type 'T))
  xs.Serialize (sw, objectToSerialize)
 finally
  sw.Close()
  sw.Dispose()
 result.ToString()
By on 6/2/2007 8:07 AM ()

Thankyou
I'v used your sample code, and it works well! I mean this:
3 let SerializeObject<'T when 'T : (new : unit -> 'T)> (objectToSerialize : 'T) : string = "fix";;

But when I add a null type constraint or "not struct" constraint, compiler gives me this errors and fails to compile:

1
2
3
4
5
"Warning: error: Couldn't strip to expected arity, expr = LAM ^T. patrhs@3, n = 1 C:\Documents and Settings\..." for this:
    3 let SerializeObject<'T when 'T : null and 'T :  (new : unit -> 'T)> (objectToSerialize : 'T) : string = "fix";;

And "syntax error. C:\Documents and Settings\..." for this:
    3 let SerializeObject<'T when 'T : not struct and 'T :  (new : unit -> 'T)> (objectToSerialize : 'T) : string = "fix";;

I can not figure it out what is wrong.
Nice Regards

By on 6/6/2007 11:51 PM ()

You've hit a couple of compiler bugs here. We'll fix them for the 1.9.2 release. if it's more urgent please contact us (fsbugs AT microsoft DOTT com).

Typically .NET "null" and "not struct" constraints aren't much use in F#, and they're only in the language because they can be inferred when using .NET code. You might like to replace this with uses of option values. Do you have a larger code sample you need to get working?

Thanks

Don

By on 6/7/2007 5:18 AM ()

Thankyou

By now, fortunatly, there is no ergent! But I hope a day for F# that such supports become ergent! :)

In fact this function is part of a toolbox that I'v written for my developing tasks in C#. I was trying to investigate in translating C# to F# and observing what aspects of code will be affected.

Yet I keep looking for next version!

Nice Regards

By on 6/7/2007 6:59 AM ()

All you have to do is remove the <'T> from SerializeObject definition. I'm sort of surprised that F# does not give an error when using the constructed type syntax in pattern definitions, but perhaps there is some use that escapes me right now.

By on 6/2/2007 7:27 AM ()
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