Re: readonly member (but assignable at constructor time)

2018-04-30 Thread bauss via Digitalmars-d-learn
On Monday, 30 April 2018 at 10:57:51 UTC, Jonathan M Davis wrote: On Monday, April 30, 2018 10:36:52 bauss via Digitalmars-d-learn wrote: On Saturday, 28 April 2018 at 04:56:26 UTC, lempiji wrote: > On Friday, 27 April 2018 at 02:59:16 UTC, Dr.No wrote: >> In C# you can have a readon

Re: readonly member (but assignable at constructor time)

2018-04-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, April 30, 2018 10:36:52 bauss via Digitalmars-d-learn wrote: > On Saturday, 28 April 2018 at 04:56:26 UTC, lempiji wrote: > > On Friday, 27 April 2018 at 02:59:16 UTC, Dr.No wrote: > >> In C# you can have a readonly member assignable either at > >> declaratio

Re: readonly member (but assignable at constructor time)

2018-04-30 Thread bauss via Digitalmars-d-learn
On Saturday, 28 April 2018 at 04:56:26 UTC, lempiji wrote: On Friday, 27 April 2018 at 02:59:16 UTC, Dr.No wrote: In C# you can have a readonly member assignable either at declaration or constructor time, like this: class C { readonly myClass mc; this() { mc = new myClass

Re: readonly member (but assignable at constructor time)

2018-04-27 Thread lempiji via Digitalmars-d-learn
On Friday, 27 April 2018 at 02:59:16 UTC, Dr.No wrote: In C# you can have a readonly member assignable either at declaration or constructor time, like this: class C { readonly myClass mc; this() { mc = new myClass(); } void doSomething() { mc = new myClass(); // wrong

Re: readonly member (but assignable at constructor time)

2018-04-26 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, April 27, 2018 02:59:16 Dr.No via Digitalmars-d-learn wrote: > In C# you can have a readonly member assignable either at > declaration or constructor time, like this: > > class C > { >readonly myClass mc; > >this() >{ > mc = new m

readonly member (but assignable at constructor time)

2018-04-26 Thread Dr.No via Digitalmars-d-learn
In C# you can have a readonly member assignable either at declaration or constructor time, like this: class C { readonly myClass mc; this() { mc = new myClass(); } void doSomething() { mc = new myClass(); // wrong! result in compiler error, mc is readonly } } Does D

Re: Readonly field for class type

2018-03-15 Thread Ali Çehreli via Digitalmars-d-learn
On 03/15/2018 03:16 AM, Andrey wrote: Hello, is there way to declare read only field for class type with ability to call inner non constant methods? i.e.: class A {     int value = 12;     void updateValue() {     value = 13;     } } class B {     const A a;     this() {     a = new

Re: Readonly field for class type

2018-03-15 Thread Seb via Digitalmars-d-learn
On Thursday, 15 March 2018 at 13:44:20 UTC, Seb wrote: On Thursday, 15 March 2018 at 10:57:52 UTC, Mike Parker wrote: On Thursday, 15 March 2018 at 10:55:16 UTC, Mike Parker wrote: class A { private int _value = 12; int value() @property { return _value; } void updateValue() {

Re: Readonly field for class type

2018-03-15 Thread Seb via Digitalmars-d-learn
On Thursday, 15 March 2018 at 13:44:20 UTC, Seb wrote: On Thursday, 15 March 2018 at 10:57:52 UTC, Mike Parker wrote: On Thursday, 15 March 2018 at 10:55:16 UTC, Mike Parker wrote: [...] Sorry. I overlooked that B.a is const. It still works, the `value` just needs to be `const` (or

Re: Readonly field for class type

2018-03-15 Thread Seb via Digitalmars-d-learn
On Thursday, 15 March 2018 at 10:57:52 UTC, Mike Parker wrote: On Thursday, 15 March 2018 at 10:55:16 UTC, Mike Parker wrote: class A { private int _value = 12; int value() @property { return _value; } void updateValue() { value = 13; } } ... auto a = new A(); writeln(a.value);

Re: Readonly field for class type

2018-03-15 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 15 March 2018 at 10:16:49 UTC, Andrey wrote: Hello, is there way to declare read only field for class type with ability to call inner non constant methods? i.e.: class A { int value = 12; void updateValue() { value = 13; } } class B { const A a;

Re: Readonly field for class type

2018-03-15 Thread Simen Kjærås via Digitalmars-d-learn
On Thursday, 15 March 2018 at 10:16:49 UTC, Andrey wrote: Hello, is there way to declare read only field for class type with ability to call inner non constant methods? i.e.: class A { int value = 12; void updateValue() { value = 13; } } class B { const A a;

Re: Readonly field for class type

2018-03-15 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 15 March 2018 at 10:55:16 UTC, Mike Parker wrote: class A { private int _value = 12; int value() @property { return _value; } void updateValue() { value = 13; } } ... auto a = new A(); writeln(a.value); a.updateValue(); writeln(a.value); Sorry. I overlooked that

Readonly field for class type

2018-03-15 Thread Andrey via Digitalmars-d-learn
Hello, is there way to declare read only field for class type with ability to call inner non constant methods? i.e.: class A { int value = 12; void updateValue() { value = 13; } } class B { const A a; this() { a = new A(); a.updateValue(); //

Why D doesn't have an equivalent to C#'s readonly?

2015-06-30 Thread Assembly via Digitalmars-d-learn
I believe it's a design choice, if so, could someone explain why? is immutable better than C#'s readonly so that the readonly keyword isn't even needed? for example, I'd like to declare a member as readonly but I can't do it directly because immutable create a new type (since it's a type

Re: Why D doesn't have an equivalent to C#'s readonly?

2015-06-30 Thread Gary Willoughby via Digitalmars-d-learn
On Monday, 29 June 2015 at 20:12:12 UTC, Assembly wrote: I believe it's a design choice, if so, could someone explain why? is immutable better than C#'s readonly so that the readonly keyword isn't even needed? for example, I'd like to declare a member as readonly but I can't do it directly

Re: Why D doesn't have an equivalent to C#'s readonly?

2015-06-30 Thread anonymous via Digitalmars-d-learn
On Monday, 29 June 2015 at 22:11:16 UTC, sigod wrote: `new immutable(MyClass)()` is invalid code. It's perfectly fine, actually.

Re: Why D doesn't have an equivalent to C#'s readonly?

2015-06-30 Thread sigod via Digitalmars-d-learn
On Monday, 29 June 2015 at 20:12:12 UTC, Assembly wrote: I believe it's a design choice, if so, could someone explain why? is immutable better than C#'s readonly so that the readonly keyword isn't even needed? for example, I'd like to declare a member as readonly but I can't do it directly

Re: Why D doesn't have an equivalent to C#'s readonly?

2015-06-30 Thread sigod via Digitalmars-d-learn
On Monday, 29 June 2015 at 22:22:46 UTC, anonymous wrote: On Monday, 29 June 2015 at 22:11:16 UTC, sigod wrote: `new immutable(MyClass)()` is invalid code. It's perfectly fine, actually. Yes, you're right. It seems I've mistyped `immutable` when was checking it with compiler.

Re: Readonly-to-outside variable

2015-04-28 Thread Baz via Digitalmars-d-learn
On Tuesday, 28 April 2015 at 19:30:06 UTC, tcak wrote: Is there any way to define a variable or an attribute as read-only without defining a getter function/method for it? Thoughts behind this question are: 1. For every reading, another function call process for CPU while it could directly

Readonly-to-outside variable

2015-04-28 Thread tcak via Digitalmars-d-learn
Is there any way to define a variable or an attribute as read-only without defining a getter function/method for it? Thoughts behind this question are: 1. For every reading, another function call process for CPU while it could directly read the value from memory. 2. Repetition of same name

Re: Readonly-to-outside variable

2015-04-28 Thread Justin Whear via Digitalmars-d-learn
can clean it up if it annoys you with something like this: mixin template readonly(T, string name) { mixin(`private T _`~name~`;T `~name~`()@property{return _`~name~`;}`); } Use it like: class Foo { // injects a private int _x, public int x() mixin readonly!(int, x); }

Re: Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-27 Thread via Digitalmars-d-learn
, the members would need to have different names: class Foo { union { private int a; public int b; } } Hm.. that doesn't provide readonly access to either a or b. But it gave me an idea: class Foo { union { private int _a; public const int

Re: Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-27 Thread AsmMan via Digitalmars-d-learn
, the members would need to have different names: class Foo { union { private int a; public int b; } } Hm.. that doesn't provide readonly access to either a or b. But it gave me an idea: class Foo { union { private int _a; public const int

Re: Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-27 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/27/14 5:48 AM, Marc =?UTF-8?B?U2Now7x0eiI=?= schue...@gmx.net Yes, that's what I originally intended. Just forgot the const, and didn't even notice it after I reread it :-P I wondered... ;) -Steve

Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-26 Thread AsmMan via Digitalmars-d-learn
I'm translated to D within my limited knowledge. I don't do much OOP, maybe it's possible and I don't know. I'm using @property to make 'a' accessible and readonly at same time but I wanted to do that without this a_ extra variable, i.e, only the methods within the Foo class can assign a new

Re: Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-26 Thread Gary Willoughby via Digitalmars-d-learn
a() { return a; } } This is the C#'s to do which I'm translated to D within my limited knowledge. I don't do much OOP, maybe it's possible and I don't know. I'm using @property to make 'a' accessible and readonly at same time but I wanted to do that without this a_ extra variable, i.e

Re: Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-26 Thread Ali Çehreli via Digitalmars-d-learn
a; this (int x) { a = 2 * x; } } void main() { Foo f = new Foo(21); writeln(f.a); // output value of a Foo f2 = f;// copy works f2 = f;// assignment works // f.a = 10; // compile error: a is readonly outside Foo's methods. } Ali

Re: Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-26 Thread via Digitalmars-d-learn
a() { return a; } } This is the C#'s to do which I'm translated to D within my limited knowledge. I don't do much OOP, maybe it's possible and I don't know. I'm using @property to make 'a' accessible and readonly at same time but I wanted to do that without this a_ extra variable, i.e

Re: Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-26 Thread bearophile via Digitalmars-d-learn
Marc Schütz: Alternatively, you could create a union with a private and a public member with the same types, but I wouldn't recommend it. Besides, the members would need to have different names: class Foo { union { private int a; public int b; }

Re: Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-26 Thread via Digitalmars-d-learn
On Friday, 26 September 2014 at 17:52:58 UTC, bearophile wrote: Marc Schütz: Alternatively, you could create a union with a private and a public member with the same types, but I wouldn't recommend it. Besides, the members would need to have different names: class Foo { union {

Re: Can I make a variable public and readonly (outside where was declared) at same time?

2014-09-26 Thread Steven Schveighoffer via Digitalmars-d-learn
{ private int a; public int b; } } Hm.. that doesn't provide readonly access to either a or b. But it gave me an idea: class Foo { union { private int _a; public const int a; } void setA(int x) { _a = x; } } Hot damn! It works too :) Can't

Re: rmdirRecurse vs readonly

2013-12-25 Thread Lemonfiend
On Tuesday, 24 December 2013 at 16:11:15 UTC, Ali Çehreli wrote: On 12/24/2013 04:13 AM, Lemonfiend wrote: std.file.rmdirRecurse refuses to remove readonly files. How would I go about deleting them anyway? Call std.file.setAttributes() first, which has apparently been added just three days

rmdirRecurse vs readonly

2013-12-24 Thread Lemonfiend
std.file.rmdirRecurse refuses to remove readonly files. How would I go about deleting them anyway?

Re: rmdirRecurse vs readonly

2013-12-24 Thread Ali Çehreli
On 12/24/2013 04:13 AM, Lemonfiend wrote: std.file.rmdirRecurse refuses to remove readonly files. How would I go about deleting them anyway? Call std.file.setAttributes() first, which has apparently been added just three days ago: :) https://github.com/D-Programming-Language/phobos/blob

How to translate C# 'readonly' keyword into D

2013-02-04 Thread o3o
I'm a C# programmer, when I apply IoC pattern I use readonly keyword (http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) in this manner: :// C# code :interface IFoo { : void Fun(); :} : :class Foo: IFoo { : void Fun() {...} :} :class Bar { : private readonly IFoo foo

Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread simendsjo
On Monday, 4 February 2013 at 09:02:31 UTC, o3o wrote: I'm a C# programmer, when I apply IoC pattern I use readonly keyword (http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) in this manner: :// C# code :interface IFoo { : void Fun(); :} : :class Foo: IFoo { : void Fun

Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread simendsjo
On Monday, 4 February 2013 at 10:26:55 UTC, simendsjo wrote: On Monday, 4 February 2013 at 09:02:31 UTC, o3o wrote: I'm a C# programmer, when I apply IoC pattern I use readonly keyword (http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) in this manner: :// C# code

Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread Jacob Carlborg
On 2013-02-04 10:02, o3o wrote: I'm a C# programmer, when I apply IoC pattern I use readonly keyword (http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) in this manner: :// C# code :interface IFoo { : void Fun(); :} : :class Foo: IFoo { : void Fun() {...} :} :class Bar

Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread o3o
On Monday, 4 February 2013 at 10:26:55 UTC, simendsjo wrote: [cut] So.. Every method you call through a const instance must also be const, otherwise you have the ability to change something that should be a constant. Thanks simendsjo, now I get it... So, let me continue the example (I remove

Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread rumbu
First, AFAIK, there is no equivalent of C# readonly in D, despite the fact that D uses 3 keywords for various kinds of immutability. Second, here you can find a mocking library for D: http://www.dsource.org/projects/dmocks/wiki/DMocks On Monday, 4 February 2013 at 13:35:24 UTC, o3o wrote

Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread Jacob Carlborg
On 2013-02-04 14:35, o3o wrote: So, let me continue the example (I remove const for simplicity)... I would like check that bar.gun() call fun() function from IFoo unittest { auto foo = new MockIFoo(); //Will not compile.Mock doesn't (yet) exist auto bar = new Bar(foo);

Re: readonly?

2012-07-12 Thread Artur Skawina
On 07/12/12 01:09, Jonathan M Davis wrote: On Wednesday, July 11, 2012 10:56:23 Artur Skawina wrote: Can anybody think of a reason to keep the current (broken) behavior? Easily. You misunderstand the current (broken) behavior part - it is about what 'C*' is, it is not at all about class

Re: readonly?

2012-07-11 Thread Tobias Pankrath
This escapes a stack reference. Ins't b supposed to be allocated on the heap?

Re: readonly?

2012-07-11 Thread Jonathan M Davis
On Wednesday, July 11, 2012 08:34:28 Tobias Pankrath wrote: This escapes a stack reference. Ins't b supposed to be allocated on the heap? The object is. The reference is not. b is taking the address of the reference, not the object. - Jonathan M Davis

Re: readonly?

2012-07-11 Thread David Nadlinger
On Wednesday, 11 July 2012 at 06:34:29 UTC, Tobias Pankrath wrote: This escapes a stack reference. Ins't b supposed to be allocated on the heap? The Bar instance is, but the pointer to it is not. Making _b a Rebindable instead of using a pointer (to what effectively is a pointer to the

Re: readonly?

2012-07-11 Thread Tobias Pankrath
On Wednesday, 11 July 2012 at 06:48:59 UTC, David Nadlinger wrote: On Wednesday, 11 July 2012 at 06:34:29 UTC, Tobias Pankrath wrote: This escapes a stack reference. Ins't b supposed to be allocated on the heap? The Bar instance is, but the pointer to it is not. Making _b a Rebindable

Re: readonly?

2012-07-11 Thread Jonathan M Davis
to be referred to by references, _not_ be pointed to by pointers. Rebindable is the correct solution to this readonly issue. - Jonathan M Davis

Re: readonly?

2012-07-11 Thread David Nadlinger
On Wednesday, 11 July 2012 at 08:56:39 UTC, Artur Skawina wrote: On 07/11/12 09:00, Tobias Pankrath wrote: Bar b = new Bar; auto b2 = b; // type of b2 is Bar* So does it meen, that a pointer of type Bar* does not point to the real object? Yeah, unfortunately. Can anybody think of a reason

Re: readonly?

2012-07-11 Thread Tobias Pankrath
On Wednesday, 11 July 2012 at 09:49:43 UTC, David Nadlinger wrote: On Wednesday, 11 July 2012 at 08:56:39 UTC, Artur Skawina wrote: On 07/11/12 09:00, Tobias Pankrath wrote: Bar b = new Bar; auto b2 = b; // type of b2 is Bar* So does it meen, that a pointer of type Bar* does not point to the

Re: readonly?

2012-07-11 Thread Artur Skawina
On 07/11/12 11:49, David Nadlinger wrote: On Wednesday, 11 July 2012 at 08:56:39 UTC, Artur Skawina wrote: On 07/11/12 09:00, Tobias Pankrath wrote: Bar b = new Bar; auto b2 = b; // type of b2 is Bar* So does it meen, that a pointer of type Bar* does not point to the real object? Yeah,

Re: readonly?

2012-07-11 Thread David Nadlinger
On Wednesday, 11 July 2012 at 10:00:33 UTC, Tobias Pankrath wrote: The languages conflates reference and instance type for classes. See here http://dpaste.dzfl.pl/a55ad2b6 . I wouldn't say this should change but it is a minor inconsistency I just stumbled on. This is not an inconsistency,

Re: readonly?

2012-07-11 Thread David Nadlinger
On Wednesday, 11 July 2012 at 10:05:40 UTC, Artur Skawina wrote: Because it doesn't let you have a real pointer to a class. What is a »real pointer«? Class references are really just pointers, in a way – you can cast them to void*. The obvious alternative would be: auto r = new Bar();

Re: readonly?

2012-07-11 Thread Ali Çehreli
On 07/11/2012 08:52 AM, David Nadlinger wrote: I fail to see anything inconsistent here. Most other operations act on the object: class B { // ... } auto b = new B(); ++b;// on the object b b; // on the object // etc. b; // on the reference That can be seen as an

Re: readonly?

2012-07-11 Thread Artur Skawina
On 07/11/12 17:54, David Nadlinger wrote: On Wednesday, 11 July 2012 at 10:05:40 UTC, Artur Skawina wrote: Because it doesn't let you have a real pointer to a class. What is a »real pointer«? Class references are really just pointers, in a way – you can cast them to void*. A real pointer

Re: readonly?

2012-07-11 Thread Jesse Phillips
On Tuesday, 10 July 2012 at 19:27:56 UTC, Namespace wrote: Maybe D need's a readonly keyword. [...] Or has D an alternative? https://github.com/D-Programming-Language/dmd/pull/3

Re: readonly?

2012-07-11 Thread Jonathan M Davis
On Wednesday, July 11, 2012 09:51:37 Ali Çehreli wrote: On 07/11/2012 08:52 AM, David Nadlinger wrote: I fail to see anything inconsistent here. Most other operations act on the object: class B { // ... } auto b = new B(); ++b; // on the object b b; // on the object // etc.

Re: readonly?

2012-07-11 Thread Jonathan M Davis
On Wednesday, July 11, 2012 23:09:17 Artur Skawina wrote: The advantages of having pointers to classes? Eg solving the problem that triggered this thread w/o hacks like ClassPtr (Rebindable is an even worse hack). [1] You'd also lose polymorphism, which you don't with Rebindable. In D,

Re: readonly?

2012-07-11 Thread Jonathan M Davis
On Wednesday, July 11, 2012 10:56:23 Artur Skawina wrote: Can anybody think of a reason to keep the current (broken) behavior? Easily. Making Object* point to the object itself rather than the reference would be so broken that it's not even funny. I can understand why you would think that

readonly?

2012-07-10 Thread Namespace
Maybe D need's a readonly keyword. Sometimes i have a class which can take an object from everywhere to store it. So it can not be const, because i didn't just initialized it with a ctor. But i don't want to change the object, i only want to read or call const methods. What now? I'd suggest

Re: readonly?

2012-07-10 Thread Simen Kjaeraas
On Tue, 10 Jul 2012 21:27:54 +0200, Namespace rswhi...@googlemail.com wrote: Maybe D need's a readonly keyword. Sometimes i have a class which can take an object from everywhere to store it. So it can not be const, because i didn't just initialized it with a ctor. But i don't want

Re: readonly?

2012-07-10 Thread Tobias Pankrath
On Tuesday, 10 July 2012 at 19:27:56 UTC, Namespace wrote: Maybe D need's a readonly keyword. Sometimes i have a class which can take an object from everywhere to store it. So it can not be const, because i didn't just initialized it with a ctor. But i don't want to change the object, i only

Re: readonly?

2012-07-10 Thread Namespace
const(T)* ? Example?

Re: readonly?

2012-07-10 Thread Ali Çehreli
On 07/10/2012 03:53 PM, Namespace wrote: const(T)* ? Example? class Bar {} class Foo { const(Bar) * _b; void SetBar(const(Bar) * b) { _b = b; } } void main() { auto b = new Bar(); auto f = new Foo(); f.SetBar(b); } Ali

Re: readonly?

2012-07-10 Thread Namespace
class Bar {} class Foo { const(Bar) * _b; void SetBar(const(Bar) * b) { _b = b; } } void main() { auto b = new Bar(); auto f = new Foo(); f.SetBar(b); } Ali Hmm... That's good. Thanks.

Re: readonly?

2012-07-10 Thread Timon Gehr
On 07/11/2012 12:58 AM, Ali Çehreli wrote: On 07/10/2012 03:53 PM, Namespace wrote: const(T)* ? Example? class Bar {} class Foo { const(Bar) * _b; void SetBar(const(Bar) * b) { _b = b; } } void main() { auto b = new Bar(); auto f = new Foo();