It isn't restructed to just operators but it does require some rather bizare syntax, the first example works because of the definition of (.[]) which has signiture:

val inline (.[]) : ^src -> 'idx -> 'elem when ^src : (member get_Item : 'idx -> 'elem)

So the constraint is actually being propogated from the use of the operator in the same way as when you do:

let inline sqr x = x * x

The constraint that x has a multiply operator is propegated to sqr, however, for the second one you are trying to access the member directly so you need to use the "strange syntax" which looks like this:

let inline length (x: ^a) =
(^a : (member get_Length : unit -> int) (x, ()) )

Unfortunatly this fails when you try to call it as "length [| |]" and I haven't been able to determine why, if you add a extra paramater to length and pass that then it will work for types with an appropriate get_Length but still fails for arrays even when passing unit as the extra paramater.

By on 10/29/2007 5:56 AM ()

Julian,

thank you for the explanation. Maybe the array problem is a bug, which someone should report.

The syntax is indeed strange. On the one hand you have the rather general syntax for type and member constraints, which should be enough to give the compiler all the information it needs, and on the other hand you can only make use of the members by employing that constraint'n'call syntax.

Stephan

By on 10/29/2007 6:30 AM ()

Hi guys,

Yes, the call syntax is verbose, somewhat deliberately at this stage.

Basically, the static member constraint mechanism was designed as a way to implement the parametrically overloaded operators used in the standard library. For this purpose the explicit syntax is ok.

The mechanism does indeed have the potential to be employed as a limited form of duck typing, but we've not yet decided if its use in this mode is something that we will encourage for broad use in the language. We'll make a decision about this during the coming months.

Note you can implement duck typing by .NET reflection, e.g. define an infix operator ($) that permits you to write things like

obj $ "Length"

However I understand this may not be efficeint enough for all purposes.

Thanks for the bug report about static member constraints for properties. I'm adding that to our bug database.

Kind regards

Don

By on 10/29/2007 6:52 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