Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
the bsd boys
Aug 8, 2011
Probation
Can't post for 349 days!

Adbot
ADBOT LOVES YOU

Zlodo
Nov 25, 2006

JewKiller 3000 posted:

for primitives it makes a copy of the value on the callee's stack. for objects it does NOT copy the object, it makes a copy of the pointer to the original heap object on the callee's stack. if the object has mutable state and the callee invokes methods that modify it, the caller WILL see those changes reflected on his side, since the new pointer copy still points to the same heap object.
however, if you take a different object and assign it to your copy of the pointer in the callee, the caller WILL NOT see that change, because the pointer was passed by value (create a copy) and not by reference (new pointer aliases original pointer). if you want a deep copy of the object for the callee that won't affect the caller, you want to use a copy constructor.

i know i need to stop posting about this but people won't stop being wrong so i can't.

see ok i admit i tend to poo poo on java without knowing that much about it (except i dont like the language and its slow), yospos bitch and all that but hold on:

a function can mutate an object passed to it? but you can overwrite it locally with another object? thats really not what id call "pass by value", rather its exactly what passing by pointer in c++ accomplishes: you have a reference to an object, but you can make it refer to a different object without the caller noticing it.
(whereas passing by value in c++ makes anything done to the object completely invisible to the caller)

its much closer to "pass by reference" than to "pass by value" tbh. earlier you said passing by ref is a bad idea because you cant locally reason about whether a function that you call will modify your object but java has an even worse version of that particular problem: you need to actually read the functions code to see if it might invoke methods that mutate your object

whereas in c++ if the signature of a function says "Butt a_butt" (pass by value) or "const Butt& a_butt" (const reference) you know that the function is not going to modify it. if it says "Butt& a_butt" (non-const reference) you know its likely to modify it (because otherwise it'd be a const ref).

JewKiller 3000
Nov 28, 2006

by Lowtax

Zlodo posted:

see ok i admit i tend to poo poo on java without knowing that much about it (except i dont like the language and its slow), yospos bitch and all that but hold on:

a function can mutate an object passed to it? but you can overwrite it locally with another object? thats really not what id call "pass by value", rather its exactly what passing by pointer in c++ accomplishes: you have a reference to an object, but you can make it refer to a different object without the caller noticing it.
(whereas passing by value in c++ makes anything done to the object completely invisible to the caller)

java is doing exactly what passing a pointer in c++ is doing. the callee can make it refer to a different object without the caller noticing. that's why it isn't pass by reference. if i have the local variables Object x = a and Object y = b, i cannot call a function swap(x, y) which results in my local state changing to x = b and y = a. pass by reference would let me write that swap function (and you can write it in C++)

Zlodo posted:

its much closer to "pass by reference" than to "pass by value" tbh. earlier you said passing by ref is a bad idea because you cant locally reason about whether a function that you call will modify your object but java has an even worse version of that particular problem: you need to actually read the functions code to see if it might invoke methods that mutate your object

that's true! mutability sucks a fat one in general :)

Zlodo posted:

whereas in c++ if the signature of a function says "Butt a_butt" (pass by value) or "const Butt& a_butt" (const reference) you know that the function is not going to modify it. if it says "Butt& a_butt" (non-const reference) you know its likely to modify it (because otherwise it'd be a const ref).

yeah java does not have the const-correctness machinery of C++. there's final but it doesn't work exactly the same way

Max Facetime
Apr 18, 2009

                          a good snipe                          



JewKiller 3000 posted:

i know i need to stop posting about this but people won't stop being wrong so i can't.

I too have the curse of having to post about IT

FamDav
Mar 29, 2008

JewKiller 3000 posted:

java is doing exactly what passing a pointer in c++ is doing. the callee can make it refer to a different object without the caller noticing. that's why it isn't pass by reference. if i have the local variables Object x = a and Object y = b, i cannot call a function swap(x, y) which results in my local state changing to x = b and y = a. pass by reference would let me write that swap function (and you can write it in C++)


that's true! mutability sucks a fat one in general :)


yeah java does not have the const-correctness machinery of C++. there's final but it doesn't work exactly the same way

i think the problem is youre really hung up on java being pass-by-value as an explicitly awesome thing when people are like "but brah i can still mutate what that object reference points to" and youre all like "but you can't mutate the reference!" and everyone is like who loving cares.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

FamDav posted:

i think the problem is youre really hung up on java being pass-by-value as an explicitly awesome thing when people are like "but brah i can still mutate what that object reference points to" and youre all like "but you can't mutate the reference!" and everyone is like who loving cares.

but it's still not pass-by-reference.

I mean, you can say it's poo poo, and I'll agree. but it's not pass-by-reference.

saying 'java is poo poo because it uses pass-by-reference semantics!' is a good way to have an argument that is completely different from the one you (presumably) intended to.

this is why words are important sometimes.

JewKiller 3000
Nov 28, 2006

by Lowtax
it's not that pass by value is awesome, just that pass by reference is worse, because it's another thing you have to worry about. also gucci posted that he preferred it and i wanted to argue against that

i care because people are misusing technical terms in a way that helps nobody and confuses people trying to learn

edit: or yeah what the guy above me said

Notorious b.s.d.
Jan 25, 2003

by Reene

FamDav posted:

i think the problem is youre really hung up on java being pass-by-value as an explicitly awesome thing when people are like "but brah i can still mutate what that object reference points to" and youre all like "but you can't mutate the reference!" and everyone is like who loving cares.

not being able to mutate the reference is itself a pretty important distinction

which is why we have the terms "pass by value" and "pass by reference"

FamDav
Mar 29, 2008

PleasingFungus posted:

but it's still not pass-by-reference.

I mean, you can say it's poo poo, and I'll agree. but it's not pass-by-reference.

saying 'java is poo poo because it uses pass-by-reference semantics!' is a good way to have an argument that is completely different from the one you (presumably) intended to.

this is why words are important sometimes.

i never said it was pass-by-reference. i'm not asking that question. that question is boring.

what i said is that passing object references (by value) leads to unintended consequences, and

JewKiller 3000 posted:

it's another thing you have to worry about.

X-BUM-RAIDER-X
May 7, 2008
cool discussion on pass by pointer semantics

double riveting
Jul 5, 2013

look at them go

Mido posted:

python is duck typed



ducks are cool


ergo


python is a messy pile of poop, never raise ducks

tcl or gtfo

X-BUM-RAIDER-X
May 7, 2008
by cool I mean who the gently caress cares shut up

Bloody
Mar 3, 2013

Deacon of Delicious posted:

so what does everyone think about semantic whitespace/brace styles/strong vs weak typing/global variables/spaces vs tabs/does java pass by reference or value/ide vs text editor

lol
allman
strongish
lol seriously
i press tab i dont give a poo poo what comes out
passes by reference by value
a little of both

X-BUM-RAIDER-X
May 7, 2008
can we just agree that Java not having const is a travesty and also that Java is a poo poo language and that c++ owns

Bloody
Mar 3, 2013

OBAMA BIN LinkedIn posted:

can we just agree that Java not having const is a travesty and also that Java is a poo poo language and that c# owns

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

OBAMA BIN LinkedIn posted:

can we just agree that Java not having const is a travesty and also that Java is a poo poo language and that python owns

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
:unsmigghh:

double sulk
Jul 2, 2010

OBAMA BIN LinkedIn posted:

can we just agree that Java not having const is a travesty and also that all programming languages are poo poo and that leaving work and going to do other poo poo owns

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

oh, good timing

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

OBAMA BIN LinkedIn posted:

can we just agree that Java not having const is a travesty and also that Java is a poo poo language and that c++ owns

I actually don't understand how const works in C++ since I'm a javatard. How does adding the keyword const prevent the caller from mutating the objects state or is is just a convention?

Nomnom Cookie
Aug 30, 2009



Const references aren't worthwhile IMO. Good code won't mutate params and bad code will, and const doesn't change that. The right thing is to make objects logically immutable as much as possible, which const doesn't help much with. poo poo, C++11 had to introduce mutable because of the difference between a const object and an immutable one.

Nomnom Cookie
Aug 30, 2009



Hard NOP Life posted:

I actually don't understand how const works in C++ since I'm a javatard. How does adding the keyword const prevent the caller from mutating the objects state or is is just a convention?

You mean callee mutating the object's state? It doesn't! If the callee wants to mutate the object they just won't declare the param as const.

FamDav
Mar 29, 2008

Nomnom Cookie posted:

Const references aren't worthwhile IMO. Good code won't mutate params and bad code will, and const doesn't change that. The right thing is to make objects logically immutable as much as possible, which const doesn't help much with. poo poo, C++11 had to introduce mutable because of the difference between a const object and an immutable one.

mutable has always existed

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
i wish your posting was muteable

Nomnom Cookie
Aug 30, 2009



FamDav posted:

mutable has always existed

I stand corrected. Const is still a bad idea, like checked exceptions.

FamDav
Mar 29, 2008

Nomnom Cookie posted:

You mean callee mutating the object's state? It doesn't! If the callee wants to mutate the object they just won't declare the param as const.

youre right, not using the keyword is exactly like not using the keyword

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

Nomnom Cookie posted:

I stand corrected. Const is still a bad idea, like checked exceptions.

oh poo poo. there goes the shaggarsignal

FamDav
Mar 29, 2008
the two interesting arguments about const/mutable keywords:

1) should you ever be allowed to mutate something
2) should you be allowed to mutate by default

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

FamDav posted:

the two interesting arguments about const/mutable keywords:

1) should you ever be allowed to mutate something
2) should you be allowed to mutate by default

1) No
2) See 1)

FamDav
Mar 29, 2008

Malcolm XML posted:

1) No
2) See 1)

same

Nomnom Cookie
Aug 30, 2009



FamDav posted:

the two interesting arguments about const/mutable keywords:

1) should you ever be allowed to mutate something
2) should you be allowed to mutate by default

Is additional enforcement necessary beyond encapsulation? The answer is no.

Nomnom Cookie
Aug 30, 2009



Talking about what languages should "allow" you to do is just another way of talking about good code and bad code. My argument is that good code will employ immutability as much as is practical, and good OOP code will have as many classes immutable as practical. In other words, controlling mutability is the responsibility of a module, not the module's users. If declaring a reference const gains you anything, well, why are you using a mutable object to do an immutable object's job?

Shaggar
Apr 26, 2006
checked exceptions are good

Stereotype
Apr 24, 2010

College Slice
const int i = 0;
i =5;

Nomnom Cookie
Aug 30, 2009



Shaggar posted:

checked exceptions are good

Nope they're bad. Like const, just whips & chains with no reacharound.

Condiv
May 7, 2008

Sorry to undo the effort of paying a domestic abuser $10 to own this poster, but I am going to lose my dang mind if I keep seeing multiple posters who appear to be Baloogan.

With love,
a mod


Hard NOP Life posted:

I actually don't understand how const works in C++ since I'm a javatard. How does adding the keyword const prevent the caller from mutating the objects state or is is just a convention?

const int* const Method3(const int* const&) const;

what's hard to understand about that?

it would be neat if final in java (and val in scala) trickled down to the members of objects so instead of having a final reference to an object, the object becomes immutable.

Shaggar
Apr 26, 2006

Nomnom Cookie posted:

Nope they're bad. Like const, just whips & chains with no reacharound.

nope. checked exceptions own forever and there is literrally no argument against them other than "I hate safety AND typing"

JewKiller 3000
Nov 28, 2006

by Lowtax

FamDav posted:

the two interesting arguments about const/mutable keywords:

1) should you ever be allowed to mutate something
2) should you be allowed to mutate by default

1) yes but only in rare cases, and before you use it, think hard about whether you really need to
2) absolutely not

Nomnom Cookie
Aug 30, 2009



Condiv posted:

const int* const Method3(const int* const&) const;

what's hard to understand about that?

it would be neat if final in java (and val in scala) trickled down to the members of objects so instead of having a final reference to an object, the object becomes immutable.

You're conflating reference mutation with value mutation. If the object is supposed to be immutable, then it will be written that way. If it's supposed to be mutable, then why the hell are you trying to make it immutable? Const StringBuilder is a ridiculous notion.

Adbot
ADBOT LOVES YOU

FamDav
Mar 29, 2008

Nomnom Cookie posted:

You're conflating reference mutation with value mutation. If the object is supposed to be immutable, then it will be written that way. If it's supposed to be mutable, then why the hell are you trying to make it immutable? Const StringBuilder is a ridiculous notion.

passing a reference by value is equivalent to passing a value by reference

also the reason const exists is to give the compiler additional information so it can catch dumb programmer mistakes. the only problem is they make you have to declare const instead of declaring mutable.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply