These are chat archives for rust-lang/rust

5th
Jan 2017
diegor8
@diegor8
Jan 05 2017 05:08
Hey everyone , phoronix posted an article a few days ago about the merits of porting a code base and has recently followed it with the news
That gnome svg library will be ported to rust
As expected , a lot of self proclaimed "real" programmers arrived inmediatlyy
To talk about how people who use any managed language in any situation are doing it wrong ( btw rust isnt managed)
But also there are more legitly seeming people posting too
Which could confuse really confuse bystanders into thinking rust is "serious" but unnecesarily hard to use or just fad , and thus to be avoided
Part of why people are saying that rust is hard to use is becuase of the lack of objeft oriebtation , whereas in rust is prereable to use component systems , which are actually easier to maintain than inhiterance graphs
Also compnents are more performat than classes
This was the first one
diegor8
@diegor8
Jan 05 2017 05:18
Please , if anyone has time , read the posts , and try comment aprooiatly , without feeding the the pro c++ trolls
diegor8
@diegor8
Jan 05 2017 05:26
If gnome is startibg to use rust , then the risk of it being a fad is over
Rust is better than c# or a lot other languages , becuase it has a chance of compeeting with c and c++ , because it has no runtime ( which means porting it , writing an os , or an app that works on bare metal is relatively easy
Also , instead of trying to be a. " reingeniered" c++
Its
It competes with it by being different , so the cost of changing language is justifyied
Because rust has no oop , but enforces component programing instead
In much better way that it could be done on c++
Because in that language if you wanted components you would probably end up implementing them using inhiterance (just look at qt3d)
diegor8
@diegor8
Jan 05 2017 05:33
Also rust has component systems that can be used across proyects , which means that procesors can be reused in different codebases
Daniil Kolesnichenko
@KolesnichenkoDS
Jan 05 2017 14:10
So you want to get the "parent" struct A from the someFunction?
I think that if you are sure that your B struct is a part of the A struct, it is possible with some unsafe hacks ([pack(c)] + casting &B to &A with std::mem::transmute)
But that is a very dirty hack, don't do that :)
Why not A::someFunction(...) instead of B::someFunction?
Matanel Levi
@matanelevi
Jan 05 2017 17:40
Why constructors dont exist in Rust? Why the 'new' function convention is better?
Diggory Blake
@Diggsey
Jan 05 2017 17:41
there's no need for constructors to be special in rust - so they aren't
Matanel Levi
@matanelevi
Jan 05 2017 17:44
@Diggsey its like a standard..
Aleksey Kladov
@matklad
Jan 05 2017 17:47

And there are some problems with constructors from the language point of view:

  • what is the value of the members in constructors body (null in Java, so calling any mehod from ctor is a trouble waiting to happen, or a separate langauge for initializer fields in C++).

  • what if you want to have several constructors with the same arguments (Joshua Bloch of effective Java advices to use factory methods instead of constructors)

  • It's difficult to do constructor-based polymorphism (contrast Rust Default and From with Java's abundance of factory objects and C++ special rules around default initialization and single argument constructors) .

  • It's difficult to signal errors from the constructor, because it must return a valid object.
Diggory Blake
@Diggsey
Jan 05 2017 18:08
also, a fun one for languages with inheritance: what is the type of this or self in the constructor?
in C++ the runtime type of this is that of the class in which the constructor is defined
in C# and Java, the runtime type of this is that of the most-derived class - the one actually being instantiated
in C++ this means that if you call virtual methods from the constructor, it won't call the method on the more derived class
Aleksey Kladov
@matklad
Jan 05 2017 18:11

in C++ this means that if you call virtual methods from the constructor

Is't this just plain UB?

Diggory Blake
@Diggsey
Jan 05 2017 18:11
only for abstract methods
in C++ an object is an instance of the base class even if a more derived class is being constructed
the derived class is then constructed in-place, over the top of the base class
in C#/Java it means that you can't properly encapsulate your classes
because the base class constructor has access to instances of your class pre-construction
Aleksey Kladov
@matklad
Jan 05 2017 18:14

in C++ an object is an instance of the base class even if a more derived class is being constructed

So, typeid will return the id of the base class?

Diggory Blake
@Diggsey
Jan 05 2017 18:14
yep
Aleksey Kladov
@matklad
Jan 05 2017 18:15
Nice to know, thanks!