How about:

1
2
3
module Constants =
   [<Literal>]
   let host = "host"
By on 3/15/2013 5:13 AM ()

Moving the binding into a module with the literal attribute does solve the issue.
Thanks!

By on 3/17/2013 7:30 PM ()

Indeed — you need to put the [<Literal>] let-binding in a module instead of in a class.

To be honest, I am surprised that a Literal as an instance let is even accepted by the compiler. An instance let is equivalent to a private member, and how can that be a literal, since there are as many instanciations of it as there are instances of the class? A [<Literal>] static let makes sense, but apparently can't be passed to an attribute either.

By on 3/16/2013 2:10 AM ()

As is typical, F#/.NET do not encode/enforce constraints on attribute use. Attributes are evil! :)

By on 3/18/2013 2:34 PM ()

Not sure if you can elaborate on this, are you saying Attributes are generally evil "and here's an example of why" or Attributes are evil in the scope of "F#/.NET do not encode/enforce constraints on attribute use"

By on 3/25/2013 12:13 PM ()

Sorry for the crypric comment.

I came to think that attributes are evil in principle, as all uses of reflection. I am obviously not yet practicing what I preach - you can see, say, WebSharper, using a lot of attributes. But for new given a choice between a design that uses reflection and attributes and a design where everything is first-class, and attributes are just API, I would be inclined to chose the latter.

In this particular case, I would prefer F# to simplify the way to express in the type system that there exists a certain static value of type 'T, instead of introducing attributes (even if these were checked).

Right now the way to approximate this (assurance that there exists a static value of type 'T) is a bit overly verbose and ugly:

1
2
3
4
5
type IProvider<'T> =
    abstract Value : 'T

let consume<'T when 'T :> IProvider<string> and 'T : (new : unit -> 'T)> () =
  (new 'T() :> IProvider<string>).Value

If we had an easy way to specify and check that a value is static/singleton in the type system, it could not only solve the need for `[<Literal>]` as well as have a few other useful applications.

By on 3/25/2013 12:31 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