D language also have distinct typedef and alias, where typedef created a completely new type, while alias was just generating a new name for the existing:
typedef int NewType;
alias int NewName;
int x = 5;
NewType t1 = cast(NewType) x; // Explicit casting required
NewName t2 = x;
+1 alias would be much clearer and type (and similar names) would be free for future use if needed (e.g. if a new
type creating alias is added or similar).
this would creates a new variable of type “type” and assigns i32 to it. It’s perfectly reasonable that this creates an alias, not a new type, like:
int value = 32;
this does not create an entirely new integer value that didn’t exist before but it’s just an alias. You can use value and 32 interchangeably. (Of course you cannot create entirely new int values at all)
But rust ist not C++, so the correct syntax for aliases (type variables) should be:
I do support this. However, to bring it a little bit of history from Haskell:
type does almost exactly what it does currently in Rust: it creates a (mostly) interchangeable alias.
newtype is a single-constructor data (an ADT, algebraic data type). It gets its own constructors, typeclasses, function signatures, etc. It’s much like saying:
struct Foo { /*...*/ };
struct Bar { pub f: Foo }
impl Bar {
fn new(f: Foo) -> Bar { Bar { f: f } }
/*...*/
}
(I think. My Haskell is a bit rusty. No pun intended.)
I’ve seen a few questions on Reddit why their new types don’t work as expected. I think this is confusing to newcomers, and I would support this change.
Will Rust ever support aliases for traits, or for lifetimes, or for something else (expressions?!)? What syntax will they have?
trait MyTrait = Trait; // Similar to the current syntax
alias MyTrait = Trait; // Similar to the proposed syntax
use Trait as MyTrait; // Syntax of module aliases reused
/*...*/; // Something completely different
And how will this thing
trait Tr {
alias Ty = int
}
be called - associated alias? : )
I’m trying to put this proposal into wider context.
type in Rust works exactly as I expect it to work, based on functional programming experience. Note that, as @Nemikolh pointed out, alias is confusing in associated type context.
Could those uses still have type while cases where it’s more of an alias to be alias? I’m just thinking out loud, associated items seem like they have a slightly different use of type.
Shouldn’t there be a difference between type and newtype?