Why do these post types conflict?

Considering the following post types:

type drCode1Body = {DrCode : byte ; Name : string ; Timestamp : DateTime ; Size : uint32 ; Options : byte}
type drCode2Body = {DrCode : byte ; LastBlock : byte ; BlockNumber : uint16 ; BlockSize : uint16 ; BlockData : array<byte>}
type drCode4Body = {DrCode : byte ; Name : string ; Timestamp : DateTime ; Options : byte ; Size : uint16 ; Data : array<byte>}

      

If I try to create instances like this (code snippets, not the complete code):

{DrCode = 1uy ; Name = name ; Timestamp = timestamp ; Size = size ; Options = options}        

{DrCode = 2uy ; LastBlock = lastBlock ; BlockNumber = blockNumber ; BlockSize = blockSize ; BlockData = blockData}

{DrCode = 4uy ; Name = name ; Timestamp = timestamp ; Options = options ; Size = size ; Data = data }

      

The first line is not allowed to be valid. Although the size on the first line is a uint32 value. He simply says, "There are no jobs for the Data field."

Changing the parameter names doesn't help either, I tried this too:

 {new drCode1Body with DrCode = 1uy and Name = name and Timestamp = timestamp and Size = size and Options = options} 

      

Then I get the following exception:

This expression is of type drCode1Body, but drCode4Body is used here

For now, I am making it clear what type of post I want here.

What's happening?

+1


a source to share


1 answer


You can just write eg.

{drCode1Body.DrCode = 1uy ; Name = name ; ...

      

to disambiguate when record field labels overlap.



(What effectively happens is that F # starts reading labels, it searches in reverse order for the first record type with those labels ... as soon as it sees DrCode and Name, it thinks "you probably mean drCode4Body!" .. . by providing diasmbiguation on the first tag of the field, you control the output type in the right direction.)

The code below compiles to F # 1.9.6.2:

#light
open System
type drCode1Body = 
  {DrCode : byte; Name : string; Timestamp : DateTime; Size : uint32; Options : byte}
type drCode2Body = 
  {DrCode : byte; LastBlock : byte; BlockNumber : uint16; BlockSize : uint16; BlockData : array<byte>}
type drCode4Body = 
  {DrCode : byte; Name : string; Timestamp : DateTime; Options : byte; Size : uint16; Data : array<byte>}
let name = ""
let timestamp = DateTime.Now 
let size = 0u
let size2 = 0us
let options = 0uy
let lastBlock = 0uy
let blockNumber = 0us
let blockSize = 0us
let blockData = [|0uy|]
let data = [|0uy|]
let r1 = {drCode1Body.DrCode = 1uy; Name = name; Timestamp = timestamp; Size = size; Options = options}        
let r2 = {DrCode = 2uy; LastBlock = lastBlock; BlockNumber = blockNumber; BlockSize = blockSize; BlockData = blockData}
let r3 = {DrCode = 4uy; Name = name; Timestamp = timestamp; Options = options; Size = size2; Data = data }

      

+3


a source







All Articles