Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to import (open) specific functions in F#?

Tags:

f#

Instead of importing a whole module, is there a way to open specific functions in another module? Something like:

open TestFuncs with [myTestFuncInOtherModule]
like image 269
Korbin Avatar asked Oct 01 '15 16:10

Korbin


People also ask

How do you import a specific function in Python?

You need to use the import keyword along with the desired module name. When interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot(.) operator along with the module name.

Can you import a function within a function Python?

Importing inside a function will effectively import the module once.. the first time the function is run. It ought to import just as fast whether you import it at the top, or when the function is run.

Can function be imported from Python file?

file.py contains a function named function . How do I import it? You should probably go through the modules section in the Python tutorial. Also if you want to import the function from the file.py , make sure there is no package in your directory with the name file .


3 Answers

As you can see in the docs, the open keyword doesn't actually load a module or namespace. It just allows you to refer to elements in that module/namespace without refering to their fully qualified name.

Being so, when you use open you're just making it easier to call the functions in that module, not actually importing/loading them in memory, so the short answer for this question is that using the open keyword you can't do this to only one function.

You can achieve the same thing easily with a let binding, though:

let f = TestFuncs.myTestFuncInOtherModule
like image 58
William Barbosa Avatar answered Nov 04 '22 16:11

William Barbosa


It's not currently possible, but I've actually put in a request for this to be added to the language. If you want to see this added in F# 4.0, one thing you could do is go vote for that request.

Another good workaround, that hasn't been mentioned yet by other answers, is to "pseudo-open" modules: assign a short name to modules whose contents you want to use. Like so:

module TP = Microsoft.FSharp.Data.TypeProviders
type mySchema = TP.SqlDataConnection<"...">
let db = mySchema.GetDataContext()

This gives you the convenience of not having to type the whole module name every time you want to reference its contents, but you maintain control of your namespace: this way there's no chance of accidental name collisions when you update a module to a new version and it adds new names.

like image 35
rmunn Avatar answered Nov 04 '22 15:11

rmunn


You can refer to particular functions in another module using full function name ModuleName.funcName:

module One =
    let square x = x * x

module Two =
    let anothersquare x = One.square x
like image 39
Petr Avatar answered Nov 04 '22 16:11

Petr