Is it possible to declare a type in a function area in F #?

Let's say I have a function that does something quite complex and is implemented using subfunctions. To simplify the task, instead of tuples, I would like to use some intermediate structures that are private to implement this function.

I do not want the declaration of these structures to flow outside. So I want something like this:

let someComplexFun p = type SomeRecord = {i:int; x:int; y:int;} type SomeOtherRecord = {...} let innerFunctionA (x:SomeRecord) = ... let innerFunctionB (x:SomeOtherRecord) = ... ... 

I tried, but of course, the compiler does not allow me to do this. I looked at the documentation and I can’t see that types should be declared at the module level.

In LISP for example, it seems that all this is completely legal, for example:

 (defun foo (when) (declare (type (member :now :later) when)) ; Type declaration is illustrative and in this case optional. (ecase when (:now (something)) (:later (something-else)))) 

So am I missing something? Is this possible if F # at all?

+7
source share
3 answers

To make sure this is not allowed according to the specification, look at the grammar of the F # expressions in the specification: Section 6: Expressions . It lists the various constructs that can be used in place of expr , and none of them are type-defn (described in Section 8: Type declarations ).

The syntax (for simplification) of the function description is let ident args = expr , so the body must be an expression (and you cannot declare types inside expressions).

+5
source

Types can only be declared in the scope of a module or namespace in F #.

(You can use access modifiers such as internal or signature files to hide types from other components.)

+1
source

What did Brian say.

heres link for more information http://www.ctocorner.com/fsharp/book/ch17.aspx

0
source

All Articles