⚠️ This article was deprecated. ⚠️
Check out the updated full article on my personal blog.
You can find even more and keep in contact with me on my socials:
For further actions, you may consider blocking this person and/or reporting abuse
Gonçalo Almeida -
Kelvin Wieth -
Ashiqu Ali -
Fernando Luca De Tena Smith -
Top comments (7)
Thank you for sharing this information!
I'm a fresh beginner with Dart and just looking into functional programming, but I was taken aback by something.
Were you trying to enforce / illustrate the immutability of the collections when you made those variables final? I find that confusing because I would've expected you to make the values (collections - map/list) final instead, on the right-hand of the assignment. I thought functional programming still allows for variables (reassigning of values). I suppose that was just a sleight of hand on your part?
Thanks again!
Hi! Sorry, I didn't understand what you mean, can you explain with a code snippet which part are you referring to?
At the end of the article, the last code sample:
final aList = [3, 6, 7, 9];
final aMap = {'name': 'Marcos', 'age': 21};
Shouldn't that instead say
var aList = final [3, 6, 7, 9];
var aMap = final {'name': 'Marcos', 'age': 21};
in order to better illustrate functional programming concepts, if that is what you were trying to do? I expected you would be making the values final, not the variables - there should still be variables in functional programming, right? That is, F.P. paradigm still allows assigning new values to variables (which is why they are called variable), I understand? Or am I taking things too far by analyzing the syntax.
Keep in mind I'm just learning Dart, so I might be wrong about some things, as in not understand some things fully/correctly.
var is used to define a variable, final is used for constants. So you can’t define a list as var and final, and variable declarations go left to right, the snippet you shared won’t work because it expects an identifier after the final keyword.
If a variable is final, its value is assigned only once. But in the case of lists or maps, you can add entries even if they’re constant (final). I highly suggest you test the code snippets you see in this and other posts on dartpad.dev or a brand new project, that way you can play with it and understand it better.
Hope that clarifies your doubt. 👍
Thank you. Yes, I did confuse terms. I was thinking of the "const" keyword. Still, the point applies - why use "constants" instead of variables, when Dart allows using const in front of a map / set / list value expression, which is what I understand FP is about? So why not enforce immutability on the values (the list/map/set etc. objects) instead by making them const?
var aList = const [3, 6, 7, 9];
var aMap = const {'name': 'Marcos', 'age': 21};
Why even make them final?
"Note: Although a final object cannot be modified, its fields can be changed. In comparison, a const object and its fields cannot be changed: they’re immutable." (from the Dart Language tour - "Final and Const" section)
We still want to have variables in a FP paradigm, if I am understanding the concepts correctly? We want immutable collections instead. Simply making variables read-only / const is not what FP is about, is it? Or maybe there's something I'm missing.
EDIT: Oops, my bad. In my defense, I'd read this past midnight originally and the last time. I see the example is using ilist/imap abstractions to make a collection immutable and work with collections as immutable. Still, I guess the use of const collections is worth consideration in the FP context?
Thanks again for your time.
This looks compelling, but I am not sure it is. Dart isn't a functional programming language, and this package, though very compelling on the surface of it, seems to help, I am not sure I would want to use it in my projects. I understand that implementing things like Either and Option is hard, but the code below I would not want to ever write, and I am not sure I would like to have it in any library included in my application
A significant effort for sure, but again, nah...
Yeah, I highly suggest checking out the package oxidized as dartz hasn't been updated in a while. The post originally was to explain how to bring functional approaches to Dart, but its use is optional, having functions as first-class citizens is the only functional characteristic by default.