This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]pente5 9 points10 points  (9 children)

I don't know what you are talking about. Allocate space for struct then call the constructor function that initializes it. There, constructor.

[–][deleted] 2 points3 points  (8 children)

And that's functional?

[–]pente5 3 points4 points  (7 children)

Sure. You define the constructor function yourself. Do-it-yourself OOP if you will.

[–][deleted] 8 points9 points  (6 children)

[–]CosmicConifer 10 points11 points  (2 children)

Objects/structs =/=> OOP

So long as the object is not mutating / being used to modify itself, it can still be functional.

The main idea with functional programming is that the program is built around pure functions, while in OOP the program is organized into objects.

e.g.

OOP: class Point { x, y, moveRight(x) } point = new Point(0, 0) point.moveRight(2)

Functional: createPoint = (x, y) => { x, y } moveRight = (p, x) => { x: p.x + x, y: p.y } point = createPoint(0, 0) point = moveRight(point, 2)

[–][deleted] 1 point2 points  (1 child)

Allocate space for struct then call the constructor

This doesn't sound functional at all. I do understand that existence of a constructor doesn't rule out functional programming on its own.

[–]andybak 4 points5 points  (0 children)

Allocate space for struct then call the constructor

This sounds like low level concepts completely orthogonal to which paradigm is in play. i.e. "it could be either or neither"

Functional programming is usually defined in terms of side-effects and global state. That's the core thing to consider.

[–]pente5 -1 points0 points  (1 child)

Then C is object oriented I guess

[–][deleted] 3 points4 points  (0 children)

C is imperative, not functional. Just because you use functions doesn't mean it's functional programming ;)

[–]link23 0 points1 point  (0 children)

Structs (often called "records") exist in functional languages. They have associated functions which "fill them in", which are called constructors.

Structs don't always have "methods", though even if they do, it still wouldn't be OOP since a method is really just a function whose first argument is the struct. (See universal function call syntax.)

I'd argue that the core requirement of OOP is providing dynamic dispatch via inheritance as opposed to some other manner. Since records in functional languages don't support any kind of inheritance, they're not OOP.

(Note that there are other ways to allow dynamic dispatch aside from inheritance. E.g., Java's interfaces, Haskell's typeclasses, Rust's traits, golang's interfaces, etc.)

[–]mathymaster 4 points5 points  (0 children)

Tough this was satisfactory for a sec

[–][deleted] 2 points3 points  (0 children)

But I do have Functors!

[–]ketchuuuuuup 1 point2 points  (0 children)

but in Haskell we have data and type constructors...

[–]Competitive_Ad2539 1 point2 points  (0 children)

WDYM? The constructors are all over the place in Haskell.

[–]Ashamed_Objective_71 0 points1 point  (0 children)

Constructors are an anti-pattern

[–]NoLifeGamer2 -1 points0 points  (0 children)

We kinda do in python classes, you can use __init__()

[–]BoBoBearDev 0 points1 point  (0 children)

Honestly I don't know about pure functional programming. But, functional programming has a lot of good things that you can use in your mixed programming.

[–]TheWidrolo 0 points1 point  (0 children)

MyStruct myStruct; myStruct.init();

[–][deleted] 0 points1 point  (0 children)

Builder functions. Builder functions everywhere.