bcarruthers / garnet

F# game composition library
MIT License
164 stars 15 forks source link

Eid unit of measure #8

Closed kerams closed 4 years ago

kerams commented 4 years ago

Hello, have you considered defining a unit of measure for Eid instead of the struct? It would be just as type-safe with the added bonus of a smaller footprint (though hardly a game changer).

Something like

[<Measure>]
type eid
type Eid = int<eid>

Reimplementing the Eid module could be a bit inconvenient because of all the bit operations where you'd have to strip the UoM and then reapply it.

bcarruthers commented 4 years ago

Thank you for the suggestion kerams!

Using units of measure would definitely take advantage of a unique F# feature. It's lightweight in the sense that it only affects compile-time checking and would be indistinguishable from using int32 at runtime.

I haven't done any benchmarks to measure the difference between using int32 directly and using it wrapped in a struct. I'd be curious to know if there are significant differences.

Both 'Eid 1' and '1' are similarly concise, although I rarely find myself constructing Eids explicitly like that. But as you mention, packing and unpacking the value can be a hassle.

One UoM downside is that anyone using the library from C# wouldn't have the benefit of strong typing since it would appear as int32. So far I haven't made C#-friendliness a priority, but I also don't want to exclude it unnecessarily.

kerams commented 4 years ago

That about C# is a fair point.