I see that in F# the type of List.length is 'a list -> int
Is it safe to assume that int will always be big enough to contain the size of any list I might create, and is that because lists are limited to 4G items?
The book I am learning from (Expert F# 2.0) says that int and int32 are t开发者_运维知识库he same size, so I set off writing my first program with a typedef for size_t. That is a pain because it requires me to explicitly tell the compiler what type constants I mean (e.g. 7UL instead of just 7), although I expect that could be avoided by turning size_t into an object with an int constructor. I also expect that would cause a range of other problems :-(
So, what is the usual solution please? Do I just use ints everywhere and ignore the 4G barrier? Array indices are ints too, so it's not like I would be able to do much useful with my size_t type even if I should be able to implement it well enough to use.
Many thanks in advance.
In .NET, you can only allocate an array that has 2GB (even if you're using 64 bit version and have enough memory). If you actually wanted to store this ammount of data, then you'd probably want to use arrays, because F# lists have larger overhead.
For this reason, you probably don't need to worry about the length being limited to 32bit int
. The only case where you would care is if you were writing data structure to workaround this 2GB limitation.
An array of 4G ints, would require 16GB of RAM, and that's only for ints (and not anything more complex). Unless you have that much memory, there's no reason to care about that limit. If you do want to use that much memory, consider splitting you array into 2-3 arrays.
In .NET, use "int" for collection sizes. It is big enough for the 99.999% case, and it is best to have standard types across all APIs.
精彩评论