Originally posted by Luke_Wolf
View Post
Announcement
Collapse
No announcement yet.
So, Microsoft just open sourced most of .NET...
Collapse
X
-
Originally posted by justmy2cents View Postyou seem to be missing parts too. there is a BIG distinction between c# class variable and class property. class variable can only contain value and initializer. while property contains automatic or custom get and set methods and initializer. another even bigger distinction is that variable won't show up in reflection while properties will. and there is shitload of reasons why would you want one or another.
I know the difference between a C# class instance variable and a class instance property. C# properties have more features that C# class instance variables, so why is the latter necessary?
Originally posted by justmy2cents View Postdon't know if it is just me, but i grew hatred for scala in manner of minutes when i looked at it long ago. maybe it is the fact that i started long ago with pascal and prefer obvious&readable over features, maybe something else.
Originally posted by justmy2cents View Posti for example can't stand naming conventions in java for the love of me.
Comment
-
Originally posted by Luke_Wolf View PostFurther there are functional requirement differences as well, with a property you want to regulate things like access and acceptable values whereas a variable does not have any such requirements placed upon it.
Comment
-
Originally posted by DanLamb View PostI understand the differences between C# class instance variables and C# class instance properties. The latter has more functionality, you can swap out a naive get/set with non-trivial get/set logic. My point is that you don't need both. When do you need a class instance variable where a property is inappropriate? What is the drawback to the Scala solution?
I decided to look up the Scala version of properties and found this http://dustinmartin.net/getters-and-setters-in-scala/
Unless you have corrections to make vs that blog, the Scala solution is exactly the same as the C# solution other than defaulting to properties as opposed to making properties optional.
And I don't know about you but I find
Code:private T _backingVariable; public T Property { get { return _backingVariable; } set { //stuff } }
Code:private var _backingVariable; def Property = _backingVariable def Property_ = //stuff;
Comment
-
Originally posted by Luke_Wolf View PostBacking variables, and other private variables that act as implementation details have no purpose being properties, if you can get rid of backing variables as a language necessity then using properties only when dealing with classes would be okay and variables only inside functions, until then I'm personally in favour of leaving it up to the developer to opt in.
Originally posted by Luke_Wolf View PostCode:private var _backingVariable; def Property = _backingVariable def Property_ = //stuff;
The Scala code you gave is a property with a custom getter/setter. In the common trivial case, where get/set have no custom logic, it is even simpler and more readable.
For a read/write property (aka variable) that can later be converted to a full geter/setter if need be without breaking binary compatibility or can be overridden in a child class:
Code:var someProp = initialValue
Code:val someProp = initialValue
Code:class MyComplex(val real: Double, val imaginary: Double) val c = new MyComplex(3.0, 4.0) c.real // 3.0 c.imaginary // 4.0
Last edited by DanLamb; 06 April 2014, 09:36 PM.
Comment
-
Originally posted by DanLamb View PostWhy can't you use a backing property in C# instead of using a backing class instance variable in C#? The property may be overkill, but you don't lose anything.
The reason for having different ways to describe "read only" is because there are different ways "read only" is actually enforced.
So for class/instance variables you have the read-only keyword which does exactly what it says. It makes these variables read-only, they are only written one, when they are initialized.
For properties you can have different settings.
For example:
Code:public int A { get; } // ---> read-only for everyone public int A { get; protected set; } // ---> read-only for everything using this class but not this class and subclasses public int A { get; private set; } // ---> read-only for everything using it, but not itself.
Comment
-
Originally posted by droste View PostNobody is stopping you from doing this in C#. I use the concept of having both of them as way to describe what is part of the interface of the class (the properties) and what is solely an implementation detail (the class/instance variables). So for me it would be a loss of information to the one who reads my code.
The reason for having different ways to describe "read only" is because there are different ways "read only" is actually enforced.
So for class/instance variables you have the read-only keyword which does exactly what it says. It makes these variables read-only, they are only written one, when they are initialized.
For properties you can have different settings.
For example:
Code:public int A { get; } // ---> read-only for everyone public int A { get; protected set; } // ---> read-only for everything using this class but not this class and subclasses public int A { get; private set; } // ---> read-only for everything using it, but not itself.
Code:class TestClass { val a = 123; } // ---> read-only for everyone class TestClass { protected var internalA = 123; def a = internalA; } // ---> read-only for everything using this class but not this class and subclasses class TestClass { private var internalA = 123; def a = internalA; } // ---> read-only for everything using it, but not itself.
Scala has an objectively, measurably simpler syntax on this issue. It uses less keywords than C#, it doesn't have the separate syntaxes like C#. Scala uses basically the same syntax for local var/val, instance var/val, and singleton var/val. It supports read-only and read-write and type inference the same way in all three situations. It requires significantly less code in almost every situation. It is a simpler syntax with more concise code that is at least as powerful.
I have never met someone who understood this issue or has used Scala more than a trivial amount and didn't think Scala was a complete 100% improvement over Java and C# on these issues. Most C# developers have never thought about this; they are happy with the tool they use and haven't considered alternatives, and often only read and talk with developers who think the same way.
Comment
-
Originally posted by DanLamb View PostWhy can't you use a backing property in C# instead of using a backing class instance variable in C#? The property may be overkill, but you don't lose anything.
Originally posted by DanLamb View PostI give you credit for actually looking it up. The Scala code is completely readable, you just aren't used to it having obviously never used Scala before.
Originally posted by DanLamb View PostThe Scala code you gave is a property with a custom getter/setter. In the common trivial case, where get/set have no custom logic, it is even simpler and more readable.
Originally posted by DanLamb View PostFor a read/write property (aka variable) that can later be converted to a full geter/setter if need be without breaking binary compatibility or can be overridden in a child class:
Code:var someProp = initialValue
Code:public T Property {get;set;} public someClassDefaultConstructor() { Property = initialValue; }
Originally posted by DanLamb View PostFor a read-only property (aka value) that can later be converted to a full geter if need be without breaking binary compatibility or can be overridden in a child class:
Code:val someProp = initialValue
Code:public T Property{get; private set;}
Originally posted by DanLamb View PostAlternatively, you can declare your class properties in the constructor:
Code:class MyComplex(val real: Double, val imaginary: Double) val c = new MyComplex(3.0, 4.0) c.real // 3.0 c.imaginary // 4.0
Code:var c = new {real = 3.0, imaginary = 4.0}; c.real; //3.0 c.imaginary; //4.0
Code:public class MyComplex { public double real{get;set;} public double imaginary{get;set;} } var c = new MyComplex (){ real = 3.0, imaginary = 4.0}; c.real; //3.0 c.imaginary; //4.0
Originally posted by DanLamb View PostThat is extremely concise, elegant, and very readable. You just aren't used to that.
Comment
-
So I decided to look at this
And it's essentially a wash for these microbenchmarks in terms of code size, for most cases the size of the program is about the same. For some cases scala has a significant advantage but in a similar number of cases C# has a significant advantage (C# saving at most 400 lines vs Scala, where Scala saves 300 lines vs C# in the best cases)...
Comment
-
Originally posted by Luke_Wolf View PostSo I decided to look at this
And it's essentially a wash for these microbenchmarks in terms of code size, for most cases the size of the program is about the same. For some cases scala has a significant advantage but in a similar number of cases C# has a significant advantage (C# saving at most 400 lines vs Scala, where Scala saves 300 lines vs C# in the best cases)...
Comment
Comment