[Mono-list] Suggestion: warning member variable not initialized
Hi, Currently the Microsoft C# compiler gives an error if you forget to initialize a readonly member variable (this can be done either in the contructor or in the declaration itself.) I already posted a wish for mcs about that, but: What about extending this behavior to non-readonly member-variables? (Maybe a warning instead of an error?) RATIONALE: I cannot think of a case where not initializing a member variable (in the constructor or at declaration time) makes sense. So we would catch a frequent semantic error without any drawback... Someone could reply that there ARE indeed some cases when you don't know an appropriate value for a member variable when the constructor is called. Maybe the user must initialize it explicitely, after construction. e.g. class C{ private int member_var; public C(){ // I don't yet know how to initialize member_var. Only the // user can do that, via the Init method. } public void Init(int m){ member_var=m; } } But, on the other hand, the above is bad design: If member_var is not always meaningful across the lifetime of class C, then member_var should NOT have been declared as a member of C, in the first place. In other words, C is a state machine with two states: Initialized and NotInitialized, such that member_var only makes sense in the Initialized state, and not in the other one. Good design: class C{ private abstract class State{} private class Initialized: State{ public int member_var; // this variable only makes sense // when C is in the Initialized state! // therefore it is declared here, // not inside C. public Initialized(int m){ member_var = m; } } private class NotInitialized: State{} private int state = new NotInitialized(); public void Init(int m){ state = new Initialized(m); } } In this latter example, member_var DOES NOT EXIST when it does make sense. This avoid the risk that someone reads/writes it when it makes no sense! In the former case, there is a time when member_var exists but does not make sense (and is not initialized). Error prone. So by raising an error in the former case we would enforce good design. Any comments? ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
Re: [Mono-list] §RE: [Mono-list] Suggestion:warning member variable notinitialized
El s?, 07-06-2003 a las 23:40, Jonathan Stowe escribió: Prints 01.01.0001 00:00:00. But try comparing that to 0 or null ;-) Try comparing it to DateTime.MinValue :-/ What Thong meant was that the initialization of those fields is equivalent to this stuff in C: typedef struct { int field1; char *field2; } X; .. X *x = malloc (sizeof (X)); memset (x, 0, sizeof (X)); .. -Gonzalo ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
RE: [Mono-list] Suggestion: warning member variable not initialized
Members are implicitly initialized to 0 or null so they do have a meaning even if you don't explicitly initialize them... ^Tum -Original Message- From: [EMAIL PROTECTED] [mailto:mono-list- [EMAIL PROTECTED] On Behalf Of Maurizio Colucci Sent: Sunday, 8 June 2003 12:50 a.m. To: mono-list Subject: [Mono-list] Suggestion: warning member variable not initialized Hi, Currently the Microsoft C# compiler gives an error if you forget to initialize a readonly member variable (this can be done either in the contructor or in the declaration itself.) I already posted a wish for mcs about that, but: What about extending this behavior to non-readonly member-variables? (Maybe a warning instead of an error?) RATIONALE: I cannot think of a case where not initializing a member variable (in the constructor or at declaration time) makes sense. So we would catch a frequent semantic error without any drawback... Someone could reply that there ARE indeed some cases when you don't know an appropriate value for a member variable when the constructor is called. Maybe the user must initialize it explicitely, after construction. e.g. class C{ private int member_var; public C(){ // I don't yet know how to initialize member_var. Only the // user can do that, via the Init method. } public void Init(int m){ member_var=m; } } But, on the other hand, the above is bad design: If member_var is not always meaningful across the lifetime of class C, then member_var should NOT have been declared as a member of C, in the first place. In other words, C is a state machine with two states: Initialized and NotInitialized, such that member_var only makes sense in the Initialized state, and not in the other one. Good design: class C{ private abstract class State{} private class Initialized: State{ public int member_var; // this variable only makes sense // when C is in the Initialized state! // therefore it is declared here, // not inside C. public Initialized(int m){ member_var = m; } } private class NotInitialized: State{} private int state = new NotInitialized(); public void Init(int m){ state = new Initialized(m); } } In this latter example, member_var DOES NOT EXIST when it does make sense. This avoid the risk that someone reads/writes it when it makes no sense! In the former case, there is a time when member_var exists but does not make sense (and is not initialized). Error prone. So by raising an error in the former case we would enforce good design. Any comments? ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
Re: [Mono-list] Suggestion: warning member variable not initialized
On Saturday 07 June 2003 16:03, Thong (Tum) Nguyen wrote: Members are implicitly initialized to 0 or null so they do have a meaning even if you don't explicitly initialize them... ^Tum I see. :-P So the semantics of readonly members is members which are not implicitly initialized, and must be explicitly initialized in the constructor... right? So the C# semantics is itself error-prone, because implicit initialization is error-prone. Of course we cannot change the semantics! :-) On the other hand, my suggestion as a warning might still be useful, maybe... bye, Maurizio ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
[Mono-list] Problems with the latest version of Mono...
Hi all, I've just checked out the very last version of Mono from CVS and compiled successfully... but as I run any Mono app (e.g. mono myApp.exe) nothing happens, or more precisely, the application does nothing and never returns. Also mcs -v never returns... My previous Mono distribution was 2 weeks old and worked fine. Any idea? Gius_. Giuseppe Greco ::agamura:: phone: +41 (0)91 604 67 65 mobile: +41 (0)76 390 60 32 email: [EMAIL PROTECTED] web:www.agamura.com ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
RE: [Mono-list] Suggestion: warning member variable notinitialized
On Sat, 2003-06-07 at 07:03, Thong (Tum) Nguyen wrote: Members are implicitly initialized to 0 or null so they do have a meaning even if you don't explicitly initialize them... ^Tum Umm... no. Some [ValueType] types don't even have an equivalent to 0 or null. e.g., System.DateTime, IIRC. Brian ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
[Mono-list] §RE: [Mono-list] Suggestion: warning member variable notinitialized
[EMAIL PROTECTED] wrote: notinitialized On Sat, 2003-06-07 at 07:03, Thong (Tum) Nguyen wrote: Members are implicitly initialized to 0 or null so they do have a meaning even if you don't explicitly initialize them... Umm... no. Some [ValueType] types don't even have an equivalent to 0 or null. e.g., System.DateTime, IIRC. class Class1 { void Method() { Console.WriteLine( t ); } /// summary /// The main entry point for the application. /// /summary [STAThread] static void Main(string[] args) { new Class1().Method(); } private DateTime t; } Prints 01.01.0001 00:00:00. -- Arild ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
Re: [Mono-list] Suggestion: warning member variable not initialized
On the contrary, structs should be designed to consider the default initialization a valid state. In particular, the default initialization sets all value type fields to their default value and all reference type fields to null. In the particular case of System.DateTime, a default-initialized instance corresponds to 1/1/0001 12:00:00 AM. On Saturday 07 June 2003 3:23 pm, Brian Deacon wrote: Umm... no. Some [ValueType] types don't even have an equivalent to 0 or null. e.g., System.DateTime, IIRC. ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
[Mono-list] classes
are dotgnu and mono classes compatibles? if so why don't both of your create another project to implement them? ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
RE: [Mono-list] Suggestion: warning member variable not initialized
Hi Maurizio, Readonly members are implicitly initialized (all memory is zeroed before it is used) but since readonly members can't be set anywhere except for in the initializer or constructor a warning is given. The compiler will actually give an warning if you never initialize a field but it won't give you an warning if you initialize it *somewhere* (doesn't have to be the constructor). So the C# semantics for all fields appear to be that a warning is given if it is read *somewhere* but is never assigned a value *somewhere*. ^Tum -Original Message- From: [EMAIL PROTECTED] [mailto:mono-list- [EMAIL PROTECTED] On Behalf Of Maurizio Colucci Sent: Sunday, 8 June 2003 2:59 a.m. To: mono-list Subject: Re: [Mono-list] Suggestion: warning member variable not initialized On Saturday 07 June 2003 16:03, Thong (Tum) Nguyen wrote: Members are implicitly initialized to 0 or null so they do have a meaning even if you don't explicitly initialize them... ^Tum I see. :-P So the semantics of readonly members is members which are not implicitly initialized, and must be explicitly initialized in the constructor... right? So the C# semantics is itself error-prone, because implicit initialization is error-prone. Of course we cannot change the semantics! :-) On the other hand, my suggestion as a warning might still be useful, maybe... bye, Maurizio ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list
Re: [Mono-list] §RE: [Mono-list] Suggestion: warning member variable notinitialized
On Sat, 7 Jun 2003, Arild Fines wrote: [EMAIL PROTECTED] wrote: notinitialized On Sat, 2003-06-07 at 07:03, Thong (Tum) Nguyen wrote: Members are implicitly initialized to 0 or null so they do have a meaning even if you don't explicitly initialize them... Umm... no. Some [ValueType] types don't even have an equivalent to 0 or null. e.g., System.DateTime, IIRC. class Class1 { void Method() { Console.WriteLine( t ); } /// summary /// The main entry point for the application. /// /summary [STAThread] static void Main(string[] args) { new Class1().Method(); } private DateTime t; } Prints 01.01.0001 00:00:00. But try comparing that to 0 or null ;-) /J\ -- Jonathan Stowe | http://www.gellyfish.com | This space for rent | ___ Mono-list maillist - [EMAIL PROTECTED] http://lists.ximian.com/mailman/listinfo/mono-list