简体   繁体   中英

Regarding F# Object Oriented Programming

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM