DEV Community

Discussion on: Functional Programming in Dart

Collapse
 
marcossevilla profile image
Marcos Sevilla

Hi! Sorry, I didn't understand what you mean, can you explain with a code snippet which part are you referring to?

Collapse
 
1n0rbits profile image
bitoolean

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.

Thread Thread
 
marcossevilla profile image
Marcos Sevilla

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. 👍

Thread Thread
 
1n0rbits profile image
bitoolean • Edited

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.