tags:

views:

412

answers:

2

There's this dichotomy in the way we can create classes in f# which really bothers me. I can create classes using either an implicit format or an explicit one. But some of the features that I want are only available for use with the implicit format and some are only available for use with the explicit format.

For example:

  1. I can't use let inline* (or let alone) inside an explicitly defined class.

  2. The only way (that I know) to define immutable public fields (not properties*) inside an implicitly defined class is the val bla : bla syntax.

    But there's a redundancy here. Since I'll end up with two copy of the same immutable data, one private, one public (because in the implicit mode the constructor parameters persist throughout the class existence)

  3. (Not so relevant) The need to use attributes for method overloading and for field's defaults is rather off putting.

Is there anyway I can work around this?

*For performance reasons

EDIT: Turns out I'm wrong about both points (Thanks Ganesh Sittampalam & MichaelGG).

  1. While I can't use let inline in both implicit & explicit class definition, I can use member inline just fine, which I assume does the same thing.

  2. Apparently with the latest F# there's no longer any redundancy since any parameters not used in the class body are local to the constructor.

  3. Will be gone in the next F# release.

+1  A: 

For let inline, you can work around by moving it outside the class and explicitly passing any values you need from inside the scope of the class when calling it. Since it'll be inlined, there'll be no performance penalty for doing this.

Ganesh Sittampalam
+4  A: 

This might not help, but you can make members inline. "member inline private" works fine.

MichaelGG
It does help. Thanks. Means I'll settle on using the explicit syntax.
Dave Berk