Hi folks and happy to have joined.
Now the pleasantries are done, , a question:
I have created the following record in an attempt to translate a C# class to F#:
type Days = Days of int
type Value = Value of int
type Item = {
Name: string
Expires: Days
Value: Value
}
Thing is I also need every Item to have aā¦ āwayā, to run another function, yet not defined, handleDevalue
, which acts on the item itself to manipulate the itemās Valueā¦ ah, value.
The handleDevalue
function is dependent on the Expires
property of the item and thus each itemās implementation of it would be different, with the only common thread being the functionās name and signature (Item -> Item
).
On the C# code Iām translating this method was defined as abstract
on the Item
class and overriden on every item instantiated (where every item is a subclass inheriting from Item
).
What Iāve tried, unsuccessfully till now:
- Add an abstract method on the record:
...} with abstract handleDevalue: Item -> Item
.
1.1 Reason for failure: IDE tells me āabstract canāt be added here as an augmentationā (or something close to the same effect). (Iām not F#-savvy enough to even know what it means, but the compiler wonāt let it compile soā¦ no). - Add
handleDevalue
as a function on the record:{... HandleDevalue: Item -> Item...}
.
2.1. Reason for failure: this function is dependent on theExpires
property. Apparently a recordās fields are mutually independent of each other, and besidesā¦ how will the function āknowā which item to act on (itās supposed to act on the item itself)? Thethis
keyword is not allowed when implementing a function when āinstantiatingā a record (i.e. no{...handleDevalue = fun this -> <some implementation code here>
). - I could remember to define the function on every item I create (I should anyway), but thatās not using the type system to my advantage. I want the compiler to force me to implement the function and remind me if I donāt.
With these ways failing Iām out of ideas how to move forward.
Thanks for any advice in advance and sorry if you meet this question on other media, Iām spreading my (dot)net wide.