Forcing a field of type F # to zero

I understand the benefits of option , but in this case I want to avoid using option for performance reasons. option wraps the type in the class, which means more work for the garbage collector - and I want to avoid that.

In this case, in particular, I have several fields, all Some under the same circumstances, but I do not want to put them in a tuple, because, again, tuples are classes - and it puts extra effort on the GC. So I get access to field.Value - which defeats the option target.

So, if there is no optimization that I don’t know about, it causes option types to be treated as links that are potentially null, I just want to use null . Is there a way I can do this?

Edit: To expand on what I'm doing, I am creating a hierarchy of bounding volumes , which is actually a binary tree with data only on leaf nodes. I implement it as a class, and not as a discriminated union, because the preservation of immutable elements is not an option to increase productivity, and discriminatory unions cannot have mutable members, only ref again, adding to Pressure in the Civil Code.

As stupid as in a functional language, I can just end each node type as an inheritance of the parent Node type. Downcasting is not the fastest operation, but with XNA and WP7, almost everything is better than defeating the GC.

+4
source share
2 answers

According to this MSDN documentation , if you decorate your type with the [<AllowNullLiteral>] attribute, you can call Unchecked.defaultof<T>() to create null for you.

This seems to be the only way inside F # to do what you want. Otherwise, you could switch to another .net language and get zeros from there ... but I assume that this is not at all what you want at all

+2
source

Now there are value options that can give you the best of both worlds.

 [<StructuralEquality; StructuralComparison>] [<Struct>] type ValueOption<'T> = | ValueNone | ValueSome of 'T 

Lack of class transfer and syntax semantics in Option<'T>

0
source

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


All Articles