Re: const in functions

2023-03-18 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 17 March 2023 at 22:41:18 UTC, Ali Çehreli wrote: My comment was purely technical. What I meant is, if the code is an island where there is no single 'const' in sight, then a D programmer can live by ignoring 'const'. At a technical level... This is different from C++ where you ha

Re: const in functions

2023-03-17 Thread Ali Çehreli via Digitalmars-d-learn
On 3/12/23 16:14, FozzieBear wrote: > On Sunday, 12 March 2023 at 15:09:45 UTC, Salih Dincer wrote: >> >> ... > > So I don't agree with part of this comment (made elsewhere in this thread): > > "You can live without 'const' until your code interacts with other > people's code." My comment was p

Re: const in functions

2023-03-13 Thread Basile B. via Digitalmars-d-learn
On Sunday, 12 March 2023 at 15:09:45 UTC, Salih Dincer wrote: Hi, [...] // A, we can get its to guarantee us that parameters // won't change: auto inConst(T)(T a, const T b) // const { // it's not needed --^ but ^-- why can't this be used Well you got the great answers to your questio

Re: const in functions

2023-03-13 Thread Ali Çehreli via Digitalmars-d-learn
On 3/13/23 08:17, Salih Dincer wrote: > In this case, using `ref` will increase performance while reducing the > number of copies. I am not sure about that. Unless there is an expensive copy construction, most objects are simple data copies. To use 'ref' or not should be guided through semant

Re: const in functions

2023-03-13 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 12 March 2023 at 19:09:13 UTC, Ali Çehreli wrote: --- In this case, using `ref` will increase performance while reducing the number of copies. Would it be wise to use `const ref` to protect the routine from ourselves or someone else? For example: ```d auto inConst( //const

Re: const in functions

2023-03-12 Thread FozzieBear via Digitalmars-d-learn
On Sunday, 12 March 2023 at 15:09:45 UTC, Salih Dincer wrote: ... So I don't agree with part of this comment (made elsewhere in this thread): "You can live without 'const' until your code interacts with other people's code." Code interacts with other code. Code should always be clear as

Re: const in functions

2023-03-12 Thread Ali Çehreli via Digitalmars-d-learn
On 3/12/23 08:09, Salih Dincer wrote: > As someone who has used const very little in my life You can live without 'const' until your code interacts with other people's code. For example, the following program works just fine: struct S { int * p; void foo() {} } void bar(S s) {} voi

Re: const in functions

2023-03-12 Thread Hipreme via Digitalmars-d-learn
On Sunday, 12 March 2023 at 15:09:45 UTC, Salih Dincer wrote: Hi, As someone who has used const very little in my life, I want to learn and ask: What are consts used in function parameters for; isn't there a copy already? Const is used for you not be able to change your values inside ref

const in functions

2023-03-12 Thread Salih Dincer via Digitalmars-d-learn
Hi, As someone who has used const very little in my life, I want to learn and ask: What are consts used in function parameters for; isn't there a copy already? ```d /* * Here's we have a class (Foo is partially simple): */ class Foo(T) { T x; // <--- Because gonna just an int fiel