How do you indicate that a function modifies it's parameters

Hi

Yes, I know it’s a bad thing :slight_smile:, but I have a function that takes a record with one of it’s fields being a C# class instance and modifies it (the class instance). This is the entire purpose of the function (apply tags to music files). My first instinct is to make it return unit result (which indicates that it has side effects), but that makes piping the function awkward (but possible). Is there a convention that I can use to indicate that this function is mutating it’s parameters while returning a record result?

Thanks

1 Like

Your first instinct was a good one, especially if there is no other output of the function. But as you say that means that you should call all your functions imperatively without piping.

Another option is the convert the tag data into an F# record and pipe it through pure functions. At the end of the pipeline, if you have an Ok you can pass the record into a function that actually performs the side-effects by updating each tag only if it changed from the original (or adding/removing tags). This the standard way to keep side effects at the edge of your program while keeping the internal logic pure.

The data passed through the pipeline could also be a Map<string, string> instead of an record, if that is rich enough to represent the tags fully. That would mean your internal functions wouldn’t need to change that much if they already edit tags directly and they would work with a closer representation to the real data, but there may be a disadvantage in clarity and type-safety of your code.

2 Likes

The reason I’m splitting it to two steps is that in most of the music tags libraries you first set the tags to a wrapper class and than you save the changes. I want to make sure all the tags are accepted on all the files and only once I know that there’s no error I want to save all the files in the next loop. but I completely agree with your about the idea of pushing changes to the edges.

Thanks

1 Like

@babysnakes I think you want to make sure all the tags are accepted on all the files and only once I know that there’s no error. By the way thanks for sharing this with us. It really helped me. :slightly_smiling_face:

Thanks @sandersbud4, I’m not sure how exactly I helped but nice to hear :slight_smile: