Is it possible to omit the class name when calling a static method?

In F #, can I omit the class name when calling a static method?

Example:

In C #, I can do something like:

using static Bizmonger.Patterns.MessageBus; ... Publish("SOME_MESSAGE"); 

instead:

 MessageBus.Publish("SOME_MESSAGE"); 

Is it possible to do something like this in F #?

+1
source share
2 answers

In F #, you can use open in namespaces (e.g. using in C #) or in modules (which is useful when the API you are calling was written in F #), but not on static classes (which is what you need when calling C # libraries).

One thing you can do to make the code a little shorter is to define an alias like:

 type M = Bizmonger.Patterns.MessageBus; // Now you can write just M.Publish("SOME_MESSAGE") // Rather than writing the full MessageBus.Publish("SOME_MESSAGE"); 

There is a function request on F # UserVoice to allow the use of open for static classes (as well as on C #), and therefore, if you want this Listen, please support and comment there.

+7
source

I also found out that I could implement a function that would serve as a wrapper for calling clients instead.

Create a wrapper function

 module Messages open Bizmonger.Patterns let Publish (message:string, payload:_) = MessageBus.Publish(message, payload) 

Client The client can then call the function without specifying a class name.

 open Messages ... Publish("SOME_MESSAGE", null); 
0
source

Source: https://habr.com/ru/post/1270360/


All Articles