I hope the FSharp team considers a more powerful literals system, perhaps inspired by Boo.

Particularly nice would be

  • timespan literals - let a, b, c = 20s, 1d, 3m
  • regular expressions - let fname, lname = /(\w+)/.Matches " Don Syme "
  • string interpolation - print "Now is ${date.Now}.";

as well as xml literals. User extensiblily would also be very nice - but I guess that takes us into the world of syntactic macros et al.

regards,

Danny

By on 3/14/2009 7:58 AM ()

Hi,
as for timespan literals, you can do something like this:

1
2
3
4
5
6
7
8
 
type System.Double with
  member x.sec = new System.TimeSpan(0, 0, int x)
  member x.mins = new System.TimeSpan(0, int x, 0)
  member x.hours = new System.TimeSpan(int x, 0, 0)


let ts = 1.0.hours + 20.0.mins + 30.0.sec

Note, you have to use doubles, because F# doesn't (currently) allow you to invoke members on int literals such as "1.ToString()" (because this conflicts with float literals "1.f" - it works if you write "1 .sec" but that looks odd). But still, I think it's pretty close to all you need to get nice numeric literal syntax! Allowing member invocation without space on integer literals would be good.

As for the other suggestions - I think these are definitely interesting options. Not sure if this is going to happen soon, but it would be useful sometimes. I guess the second example would give you "incomplete pattern matching warning" because you cannot be sure that it will return two values (or maybe it could require some active pattern to extract the values).

T.

By on 3/14/2009 9:03 AM ()

Nice tip Tomas!

By on 3/15/2009 8:28 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