I think it goes as below (don't have CTP handy to try):

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

open System

[<AttributeUsage(AttributeTargets.Constructor, Inherited = false, AllowMultiple = false)>] 

type public FooAttribute() = inherit System.Attribute() 

type Baz [<Foo>] (bar: int) = 

   let _bar = bar

   member this.Bar = bar
By on 4/16/2009 4:50 PM ()

That's what I thought, but no-go:

- type Baz [<Foo>] (bar: int) =

type Baz [<Foo>] (bar: int) =
-----------------^^

stdin(7,18): error FS0010: Unexpected symbol '(' in definition

By on 4/16/2009 5:14 PM ()

Yeah, this might just be missing or broken in the CTP. It's coming.

Note that to 'hide' an implicit constructor (e.g. when it's an implementation detail and you are using explicit constructors, e.g. for attributes today), you can just declare the implicit constructor private at point (*2*) as mentioned here

[link:lorgonblog.spaces.live.com]

By on 4/16/2009 5:27 PM ()

Fair enough, thanks a lot for trying Michael & Brian.

By on 4/16/2009 6:46 PM ()

Good question! I have no idea :). "constructor" is a keyword for attributes, but right now it just generates a warning that it's reserved, and I think the specs say it's for union case constructors.

The specs also say you can stick attributes on the .fsi interfaces, but I haven't been able to get FSC to actually emit them.

Anyways, here's a hack that might help you out if you're stuck. You can use interfaces to hide the implicit constructor, then apply the attribute to an explicit one:

Program.fsi:

#light "off"

module Program
[<System.AttributeUsage (enum<System.AttributeTargets> (32))>]
type FooAttribute =
class
inherit System.Attribute
new : unit -> FooAttribute
end
type Baz =
class
[<FooAttribute()>]
new : bar:int -> Baz
[<DefaultValueAttribute()>]
member Test : int
end

Program.fs:

#light
module Program

open System

[<AttributeUsage(AttributeTargets.Constructor, Inherited = false, AllowMultiple = false)>]
type public FooAttribute() = inherit Attribute()

type Baz (bar : int, dummy) =
member x.Test = bar
[<Foo>]
new(bar) = Baz(bar, 0)

You end up with a public interface that's what you want, and you get to keep the implicit constructor syntax. But yea, it's annoying and ugly :).

By on 4/16/2009 4:43 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