0

I'm trying to create a communication library that interacts with hardware. The protocol is made up of byte arrays with a header (source/destination address, command number, length) and a command specific payload. I'm creating Record Types for each of the commands to make them more user friendly.

Is there a more idiomatic way of converting an array to a record than

let data = [0;1]
type Rec = {
 A : int
 B : int
}
let convert d =
 {
 A = d.[0]
 B = d.[1]
 }

This can become very tedious when the records are much larger.

asked Mar 11, 2015 at 15:01

1 Answer 1

2

A few comments:

You record type definition is bogus - there should be no = in there. I assume you want

type Rec = {
 A : int
 B : int
}

You mentioned byte arrays, but your data value is a List. Accessing List items by index is expensive (O(n)) and should be avoided. If you meant to declare it as an array, the syntax is let data = [|0;1|]

But I wonder if records are the right fit here. If your goal is to have a single function that accepts a byte array and returns back various strongly-typed interpretations of that data, then a discriminated union might be best.

Maybe something along these lines:

// various possible command types
type Commands =
 | Command1 of byte * int // maybe payload of Command1 is known to be an int
 | Command2 of byte * string // maybe payload of Command1 is known to be a string
// active pattern for initial data decomposition
let (|Command|) (bytes : byte[]) =
 (bytes.[0], bytes.[1], Array.skip 2 bytes)
let convert (bytes : byte[]) =
 match bytes with
 | Command(addr, 1uy, [| intData |]) ->
 Command1(addr, int intData)
 | Command(addr, 2uy, strData) ->
 Command2(addr, String(Text.Encoding.ASCII.GetChars(strData)))
 | _ ->
 failwith "unknown command type"
// returns Command1(0x10, 42)
convert [| 0x10uy; 0x01uy; 0x2Auy |]
// returns Command2(0x10, "foobar") 
convert [| 0x10uy; 0x02uy; 0x66uy; 0x6Fuy; 0x6Fuy; 0x62uy; 0x61uy; 0x72uy |]
answered Mar 11, 2015 at 18:25
1
  • Exactly what I was looking for. Sorry I was typing on my phone and wasn't paying attention to syntax at all. Commented Mar 11, 2015 at 18:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.