F# declare variable without instatiating it

I need to declare a variable in F# without instantiating it. This is simple in C#, but in F#, all I have is the val keyword, and that only works within classes. Is there a way to declare a variable for scope purposes, but to leave the initialization for later?

EDIT: To answer Fyodor's question:

I want to programmatically go through a file folder with C# serialization files, each of which holds an array of either type A or type B objects. Each of these objects has a timestamp, and there is one C# array file per day. So let's pretend there are 20 files in all (10 A files and 10 B files), and within each file, there are 1000 A objects, or 1000 B objects, each object having a timestamp, and each file collecting 1000 data points per day.

The problem is that type A files and type B files are started and stopped at different points throughout the day, so I can't just pick Day1A and Day1B, merge them, then pick Day2A and Day2B, and so on. I need to go through Day1A and Day1B until one of them finishes (say A finishes first), then load Day2A and compare Day2A and Day1B until one of them finishes, ect.

To do this, I need to enumerate over the 1000 objects in each file, as well as the files themselves. I also need to handle weird cases where there are no type A's or type B's left (in which case I just enumerate over the remaining type).

The scope comes into play because I have two sets of enumerators going over two sets of while loops.

-------------Problems Reply------------

There are only a very few cases where you would actually need this in F#. Most likely, you can find another (more functional and more idiomatic) way of doing this.

For example, in C# you might often need to declare a variable for scope purposes if you want to initialize it using if:

int some;
if (Condition()) { some = 1; }
else { Console.WriteLine("yay"); some = 2; }

But you don't need to do this in F#, because if is an expression:

let some =
if Condition() then 1

So, if you describe the concrete situation you are facing, you will probably get a more useful answer with a nicer idiomatic F# solution.

Values in F# are immutable, so there is no ability to change the value. Thus there is no means to do what you want directly.

Rather one would normally create a "ref cell" – a container for a variable – which does allow the content of the value to be changed. However you will still need to initialise the cell. (See the link for an example.)

Better would be to refactor the code to avoid the need (not always possible, but F# has sufficient syntax that this is possible far more than in imperative languages like C#).

Category:f# Views:2 Time:2016-02-25

Related post

  • Prevent declaring variables without var 2011-02-01

    How can I prevent variables from being declared without using the var keyword? Can I set NetBeans to warn me about this? For example, I want a warning or something on this: var testing = 5; // ... testin = 2; // Woops, typo! --------------Solutions--

  • Declaring variables without var keyword 2011-07-31

    At w3schools there is written: If you declare a variable, without using "var", the variable always becomes GLOBAL. Is it useful to declare global variable inside the function? I can imagine to declare some global variables in some event handler, but

  • Declare variable without var keyword and logical OR 2011-08-10

    I get a strange behavior when declaring an object with the logical OR. my_var = my_var || {}; // throws TypeError If I add the var keyword var my_var = my_var || {}; // returns empty object Why is this? I can't seem to find an explanation. my_var is

  • Declaring variable without var sign in beginning? 2011-08-18

    I have seen class that states var _hello_kitty = array(); why they dont use $? i tried to make it public and static and it does not work without adding $i.e. public static $_hello_kitty = array(); now when i do add $ other functions who reference it

  • Declare a table variable without column definitions? 2010-11-10

    Is there a way, in SQL Server, to declare a table variable without knowing the table definitions? Exempli gratia: DECLARE @Results TABLE INSERT INTO @Results EXEC MyProc @param1 = @myValue or DECLARE @Results TABLE SELECT INTO @Results EXEC MyProc @p

  • Managing without Objects in C - And, why can I declare variables anywhere in a function in C? 2011-05-11

    everyone. I actually have two questions, somewhat related. Question #1: Why is gcc letting me declare variables after action statements? I thought the C89 standard did not allow this. (GCC Version: 4.4.3) It even happens when I explicitly use --std=c

  • Can I use a variable without declaring by $this->myvar in php? 2011-05-13

    In the following code, variables user and permissions are not declared at the beginning like $data, $module etc. And these $this->user and $this->permissions are used in the extended class of this class. My question is can I use variables witho

  • Is it possible only to declare a variable without assigning any value in Python? 2009-03-19

    Is it possible to declare a variable in Python, like so?: var so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why? EDIT: I want to do this for cases like this: valu

  • In C++, is it possible to get the return type of a function in order to declare a variable without calling that function? 2011-12-21

    int myfun() { return 42; } I know I can write auto myvar = myfun(); but what if I just want to declare myvar (without using a common typedef)? the_type_returned_by_myfun myvar; What can be written instead of the_type_returned_by_myfun? --------------

  • Locally declared variables can not be inspected 2008-11-07

    Sometimes when I am debugging code in Eclipse it happens that although I can see and inspect class member variables without any difficulty I am unable to inspect the values of variables declared locally within functions. As an aside, any parameters t

  • When should I declare variables in a PHP class? 2009-07-06

    I'm new to the OOP paradigm, so there's probably a simple explanation for this question... Do you always need to declare public object-wide variables in a class? For example: <?php class TestClass { var $declaredVar; function __construct() { $this

  • Declaring variables inside a switch statement 2009-08-05

    This question already has an answer here: Why can't variables be declared in a switch statement? 23 answers I saw a few answers to this issue, and I get it — you can't declare and assign variables inside a switch. But I'm wondering if the following i

  • Is using 'var' to declare variables optional? 2010-03-21

    Is "var" optional? myObj = 1; same as ? var myObj = 1; I found they both work from my test, I assume var is optional. Is that right? --------------Solutions------------- They mean different things. If you use var the variable is declared within the s

  • How did it happen that "static" denotes a function/variable without external linkage in C and C++? 2010-05-06

    In C static can mean either a local variable or a global function/variable without external linkage. In C++ it can also mean a per-class member variable or member function. Is there any reference to how it happened that the static keyword that seems

  • Declare variables at top of function or in separate scopes? 2010-09-22

    Which is preferred, method 1 or method 2? Method 1: LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch (msg) { case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; RECT rc; GetClientRect(hwnd, &rc); hdc = BeginPaint(hw

  • Can I declare variables in-line instead of at the top of a function? 2010-11-09

    I have used visual basic about 5 years ago. but i have started using delphi 5 years ago (when most developers jumped from delphi to visual studio) delphi is as easy as vb and at the same time it is rad and robust. Delphi is having many changes since

  • JavaScript - select which variable to pass based on the value of another variable (without using an if) 2010-11-28

    I would like to choose which variable to pass to a funciton based on the value of another variable, without using an IF or switch statement. For example, if var1 = yellow, than pass variable yellow. If var1 = red, then pass variable red. But without

  • Is it a good/acceptable practice to declare variable as interface type? 2010-12-16

    Is it a good practice to declare variable using the interface? In my company we had a discussion on it and I'm quite against it. e.g. I wanted to have a collection storing string keys and string value pairs. I don't want to allow duplicates either. S

  • why value types are declared (instantiated) without 'new' keyword in (vb or C#).net 2011-01-19

    why value types are declared (instantiated) without 'new' keyword in (vb or C#).net --------------Solutions------------- Value types are not necessarily constructed without the use of the new keyword, but there's support in the language for primitive

  • relevance of declaring variable in perl 2011-03-19

    In Perl programming, what is the relevance of variable declaration, when we can change value contained by it? e.g. my $name ="johny"; In the next statement we can change type of value contained by it with: $name = 10; Then what is relevance of declar

  • Can I store a string into a single field in mySQL that I can echo in PHP, that also declares variables? 2011-04-16

    I have a .php page that has variables, each associated with dozens of different search criteria. I thought it would be easiest to just jam all the variables into the database as one gigantic string. Then I thought it would be even EASIER to jam the v

  • using declare variables in ORACLE 2011-05-11

    Hope all doing well. I am having an sysntax issue with the declaration in Oracle. I use these variables in MS SQL server and they work just fine. How do i declare these in Oracle Use in MS SQL server: DECLARE @FROM_DT DATETIME DECLARE @END_DT DATETIM

  • Declaring variables jQuery and JS 2011-05-18

    It may be a silly question but I haven't been able to find any documentations on this on the internet. When declaring variables to use within Javascript i normally use var x = 0 but I've seen in jQuery tutorials that they use $x = 0. What is the diff

  • Variable without var 2011-06-24

    Regarding, Local variables ... function foo(){var x = 1; ...} ...and Global variables, ... (global, example 1) function foo(){x = 1; ...} (global, example 2) var x=1; function foo(){...} ...the only (simple) combination left is: x=1; function foo{...

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.149 (s). 11 q(s)