F# beginner here.
Did I say beginner ? This is my second day with F# )
I want to make only two states for record: Valid and InValid;
I'd like to make illegal state unrepresentable.
Here is my code:
module Foo =
type Success = Success of string
type Pending = Pending of string
type Failure = Failure of string
type Valid = Valid of string
type InValid = InValid of string
type StateGranted = { Status: Valid; Msg: Success }
type StateDenied = { Status: InValid; Msg: Failure }
type State =
| StateGranted
| StateDenied
let status = Valid "Valid"
let msg = Success "Success"
let state = { Status = status; Msg = msg } // I have an error here
printfn "result %A" state
So, how can I create record of type State ?
If user wants to create
let state = { Status = Valid; Msg = Pending } // compiler should throw an error here
I have TypeScript background
P.S. I have found very useful article but I don't understand it yet
I'd suggest tackling a project in F# where it's clear what you want to achieve. You need to make it clear to yourself what the legal data is.
It's not clear what the Success type represents. Is Success "Invalid" legitimate? This goes for all of the first five types and these can be deleted.
When you are trying to define state you probably intend for both StateGranted and StateDenied to be valid possibilities. But these are not of the same type. You need to define a single type that represents the things that state could be.
This reduces your code to something like:
[<RequireQualifiedAccess>]
type State =
| Granted // possibly you mean for there to be string-like data here
| Denied
// or possibly there are string functions of it like
//member t.ValidityString =
// match t with
// | Granted -> "Valid"
// | Denied -> "Denied"
You have an error because the type inference cannot distinguish between the two records of different types but same properties. You need to prefix one of the members:
let state = { StateGranted.Status = status; Msg = msg }
Related
I have 3 state machines that follow the similar model:
- same states
- same calls for each state
only the implementation of these calls change.
All the data is carried in a single record that also contains a DU that has the state and there are also a few other common parameters. Each implementation also adds their own fields to that record.
So I would like to figure out if there is a way to make a record I can extend but that contains a number of common fields that the code is expecting to find. Something similar to an interface for a type, but for a record.
For example:
type State =
| a
| b
type StateMachine1StateObject =
{
State: State
SomeData: ...
}
type StateMachine2StateObject =
{
State: State
SomeOtherKindOfData: ...
}
and I would like to have a single state machine loop (which is really a mailbox processor loop) take all the same messages, and be able to rely on some fields that will always be there in that type, but the records themselves may be different as they contain their own fields.
How can this be structured? It looks like I can't use interfaces with records nor combine multiple records into one.
If you have a lot of data in common then can create for example a record, with a field, that has a generic type.
type State<'a> = {
Name: string
Id: int
Extended: 'a
}
type Person = {
FirstName: string
LastName: string
}
type Computer = {
CPU: string
Ghz: float
}
This way, you can create a state that have different Extended data
// State<Person>
let x = {
Name = "foo"
Id = 1
Extended = {
FirstName = "David"
LastName = "Raab"
}
}
// State<Computer>
let y = {
Name = "bar"
Id = 2
Extended = {
CPU = "AMD"
Ghz = 3.6
}
}
You also could convert from one record to another. Your state machine, only accepts a record with those data it needs. For exmaple, you have a state machine that only needs Name and Id. You create.
type StateA = {
Name: string
Id: int
}
Assume you now have a different state with shared keys.
type StateB = {
Name: string
Id: int
Extra: float
}
Instead of type hackery, generics and so on, the easiest way is just to convert from B to A.
let stateBtoA (record:StateB) : StateA = {
Name = record.Name
Id = record.Id
}
So your state machine only accepts StateA, and you have a bunch of function, and other types must be converted to StateA before passing it to the state machine.
If for some reason your state machine, needs access to the orignal data, you should be able to pass it as a generic paramter to a function call. Like
message (x:StateA) (y:'a) : 'a =
// do something with x
y
message (stateBtoA data) data
So your function could work on the first argument, that is of StateA, and for example returns the second argument y that could be StateB. As long your function does nothing with y. It should be generic.
In Common.fs:
namespace Bug
module Common =
type SourceEntity = {
id : int
link : string
}
type ReleaseEntity = {
id : int
notes : string
}
In Release.fs
namespace Bug
open System
open Common
module Release =
let cache = new Collections.Generic.Dictionary<int, ReleaseEntity>()
let AddToCache(entity) =
cache.Add(entity.id, entity)
()
let AddRec() =
let entity : ReleaseEntity = {
id = 1
notes = "Notes"
}
AddToCache(entity)
In Source.fs
namespace Bug
open System
open Common
module Source =
let Cache = new Collections.Generic.Dictionary<int, SourceEntity>()
let AddToCache(entity) =
Cache.Add(entity.id, entity) <<=== E R R O R
()
let AddRec() =
let ent : SourceEntity = {
id = 1
releases = "Releases"
}
AddToCache(ent) <<=== E R R O R
Files included in above order in the Visual Studio project.
Error reported in Source.fs:
Error FS0001 This expression was expected to have type
'SourceEntity'
but here has type
'ReleaseEntity'
If the order of the two types in Common.fs are reversed, the error is reported in Release.fs where expected type is ReleaseEntity but has type SourceEntity.
Any ideas why this error is happening?
It's a clash (and shadowing) of record field names.
When you write entity.id in the body of Bug.Source.AddToCache, the compiler uses the fact that you're accessing the .id field to infer the type of entity. Which records have a field named id? Well, those two records do, but the compiler has to pick one. How? Easy: the last one takes precedence. This is called "shadowing".
In order to disambiguate the choice, just add a type annotation:
let AddToCache(entity) =
Cache.Add(entity.id, entity)
()
Wait, but why doesn't the compiler use the type of Cache.Add to infer the type of entity?
Well, this is just a limitation (or a feature?) of F#. The compilation is single-pass, type interference proceeds top down, left to right, without doublebacks. This allows the compiler to be very fast and very predictable (looking at you, Haskell).
But in this case it means that by the time the compiler sees that entity is used as parameter in Cache.Add, it has already decided what its type must be.
When you get a type error, try to think about how the compiler came to infer that particular type.
Here:
let AddToCache(entity) =
cache.Add(entity.id, entity)
()
Could the compiler know which type has would have an id field in entity?
If you had typed in
let entity = { id = 1; link = "" }
the compiler would infer that this is SourceEntity because only SourceEntity has those particular record fields. In cache.Add(entity.id, entity), the compiler has no other constraints to go by, other than it has to have an id field, so it picks the last matching type - and that is why you get the error.
If you refactor the common id field to
namespace Bug
module Common =
type SourceEntity = {
source_id : int
link : string
}
type ReleaseEntity = {
release_id : int
notes : string
}
you will find that the error disappears.
Solutions
All of the solutions involve constraining it to a known type.
The simplest is to add a type annotation:
let AddToCache(entity: SourceEntity) =
Another is to deconstruct it explicitly:
let { SourceEntity.id = id } = entity
Cache.Add(id, entity)
Another is to coerce the type - this isn't relevant here, but it may come to be useful down the road:
Cache.Add((entity :> SourceEntity).id, entity)
I'd recommend this article from F# for fun and profit on type inference for a nice explanation of the process.
P.S.
You actually only needed that one type annotation.
The rest can be inferred :)
module Source =
let Cache = new Collections.Generic.Dictionary<_, _>()
let AddToCache (entity: SourceEntity) =
Cache.Add(entity.id, entity)
()
let AddRec () =
let ent = {
id = 1
link = ""
}
AddToCache(ent)
How do I create some string from a string?
I'm going through the F# Koans tutorials, and am I stuck on this one:
[<Koan>]
let ProjectingValuesFromOptionTypes() =
let chronoTrigger = { Name = "Chrono Trigger"; Platform = "SNES"; Score = Some 5 }
let halo = { Name = "Halo"; Platform = "Xbox"; Score = None }
let decideOn game =
game.Score
|> Option.map (fun score -> if score > 3 then "play it" else "don't play")
//HINT: look at the return type of the decide on function
AssertEquality (decideOn chronoTrigger) (Some "play it")
AssertEquality (decideOn halo) (Some "don't play")
the exception i get is:
You have not yet reached enlightenment ...
Expected: null
But was: <Some(don't play)>
How do I upcast a string to be of type option string?
How do I upcast a string to be of type option string?
Casting has a very specific meaning. What you want to do is wrap your string with an Option, not cast it. To do this, use the Some constructor function:
let x = Some myString //x: string option
However, I don't think that is going to fix assertion error you're getting (at least, not by itself). I don't want to give you the complete answer here (especially since that's not what you're asking and finding the answer is the entire point of doing a Koan) but I will leave this clue as to why you're seeing a null in the assertion:
None |> printfn "Value: %A" // Value: <null>
See Why is None represented as null? for some more information on that behavior.
I'm having a problem getting my DU working as expected. I've defined a new DU which either has a result of type <'a> or any Exception derived from System.Exception
open System
// New exceptions.
type MyException(msg : string) = inherit Exception(msg)
type MyOtherException(msg : string) = inherit MyException(msg)
// DU to store result or an exception.
type TryResult<'a, 't> =
| Result of 'a
| Error of 't :> Exception
//This is fine.
let result = Result "Test"
// This works, doing it in 2 steps
let ex = new MyOtherException("Some Error")
let result2 = Error ex
// This doesn't work. Gives "Value Restriction" error.
let result3 = Error (new MyOtherException("Some Error"))
I can't understand why it is allowing me to create an "Error" if I do it in 2 steps, but when i'm doing the same thing on a single line, I get a Value Restriction error.
What am i missing?
Thanks
UPDATE
Looking at the post by #kvb, adding type information each time I need to create an Error seemed a bit verbose, so I wrapped it up into an additional method which creates an Error and is a bit more succinct.
// New function to return a Result
let asResult res : TryResult<_,Exception> = Result res
// New function to return an Error
let asError (err : Exception) : TryResult<unit,_> = Error(err)
// This works (as before)
let myResult = Result 100
// This also is fine..
let myResult2 = asResult 100
// Using 'asError' now works and doesn't require any explicit type information here.
let myError = asError (new MyException("Some Error"))
I'm not sure if specifying an Error with 'unit' will have any consequences I haven't foreseen yet.
TryResult<unit,_> = Error(err)
Consider this slight variation:
type MyOtherException(msg : string) =
inherit MyException(msg)
do printfn "%s" msg
let ex = new MyOtherException("Some Error") // clearly, side effect occurs here
let result2 = Error ex // no side effect here, but generalized value
let intResults = [Result 1; result2]
let stringResults = [Result "one"; result2] // can use result2 at either type, since it's a generalized value
let result3 = Error (MyOtherException("Some Error")) // result would be of type TryResult<'a, MyOtherException> for any 'a
// In some other module in a different compilation unit
let intResults2 = [Result 1; result3] // why would side effect happen here? just using a generic value...
let stringResults2 = [Result "one"; result3] // likewise here...
The issue is that it looks like result3 is a value, but the .NET type system doesn't support generic values, it only supports values of concrete types. Therefore, the MyOtherException constructor needs to be called each time result3 is used; however, this would result in any side effects occurring more than once, which would be surprising. As Ringil suggests, you can work around this by telling the compiler to treat the expression as a value anyway:
[<GeneralizableValue>]
let result3<'a> : TryResult<'a,_> = Error(new MyOtherException("Some Error"))
This is fine as long as the constructor doesn't have side effects.
You can do:
let result3<'a> = Error (new MyOtherException("Some Error"))
EDIT:
As for why you can't do it in one step, first note that this results in the same error:
let result4 = Result (new MyOtherException("Some Error"))
As does this:
let result4 = Result ([|1;|])
But that this works:
let result4 = Result ([1;])
What's similar about Exception and Arrays, but not Lists? It's their mutability. The value restriction will bother you when you try to do make a TryResult with a type that is mutable in a single step.
Now as for why the two step process solves this, it's because the constructor make the whole function not generalizable because you're applying a function to the constructor. But splitting it into two steps solves that. It is similar to Case 2 here on MSDN.
You can read more about it at the above MSDN article and the why this happens in this more indepth blog post.
I have some hardware connected to my PC. The hardware consists of two components each with the ability to read some stuff from its surroundings (e.g. temperature).
I communicate with the hardware using a protocol and I have implemented that protocol in F# and I have this (simplified) model:
I can query the hardware about values/readouts of the each of the sub components and each sub component has different values I can query with different types.
type ChannelAResponse =
| Data1 of float
| Data2 of string
type ChannelBResponse =
| Data1 of int
| Data2 of string
type Response =
| ChannelA of ChannelAResponse
| ChannelB of ChannelBResponse
type ResponseMessage =
{ Id : int
Response : Response }
// The `msg` is actually constructed from the data sent from the hardware
// where this `msg` is just an example.
let response = Response.ChannelB <| ChannelBResponse.Data2 "Everything ok"
let msg = {Id=10; Response=response}
A this point I know - from the protocol - that response is Response.ChannelB and it's data is ChannelBResponse.Data2.
Even though I know this, I still have to do something like following to get the actual string value out
let data = match msg.Response with
| Response.ChannelB x ->
match x with
| ChannelBResponse.Data2 y -> y
which is sort of OK because the protocol guarantees that match doesn't fail, but it's cumbersome to write this for all possible combinations.
Is there an easier way to "cast" the msg.Response into a string in this case?
This is equivalent to your incomplete pattern match, but shorter
let (ChannelB(Data2 data)) = msg.Response
// val data : string = "Everything ok"
warning FS0025: Incomplete pattern matches on this expression. For example, the value 'ChannelA (_)' may indicate a case not covered by the pattern(s).