For example, Rust is totally okay with this, and it's not because the 0 is naturally typed as u64:
fnmain() {
let a = 0;
letmut b = a + a;
let c = a + b;
b += c;
let d: u64 = c*b;
println!("d is {}", d); // Compile error
}
In contrast, with Scala, once you do val a = 0 it fixes the type as Int. If you want it to be something else, you have to say so there.
Klas Segeljakt
@segeljakt
I thought it had affine types
Ichoran
@Ichoran
Yeah, it has, actually, something a little bit more complicated than either affine or linear types.
But it's the same essence.
Klas Segeljakt
@segeljakt
ownership + move = affine? but then you also have copy
and borrow
Ichoran
@Ichoran
It's affine-like, but there's the whole single-mutable-borrow-or-arbitrary-immutable-borrow thing.
Affine means "may use it at most once".
OlegYch
@OlegYch
borrow checking is pretty central to rust and i'd guess whatever flavor of substructural typing it has just naturally flows from that
Ichoran
@Ichoran
Linear, strictly speaking, means "must use it exactly once"
Klas Segeljakt
@segeljakt
I have tried to understand the “Region inference” for determining lifetimes
but I don’t get it
or, I get the general concept
Ichoran
@Ichoran
You really shouldn't, unless you're working on implementing the lifetime system. Just like you shouldn't try to understand explicitly all the steps the Scala compiler uses to do type inference.
Having a practical knowledge of how to get the languages to do what you want, or close enough, is fine.
_
Klas Segeljakt
@segeljakt
I am working on a master thesis for embedding Rust as a DSL in Scala
Ichoran
@Ichoran
In each case, the implementation details encompass enough tricky subcases that just hoping that....ah...you...what?!
Fabio Labella
@SystemFw
:)
Klas Segeljakt
@segeljakt
but I skipped the ownership system entirely
Ichoran
@Ichoran
Oh man. Well, you have your work cut out for you. For starters, the basic syntax is irreconcilably incompatible.
Klas Segeljakt
@segeljakt
xD
Ichoran
@Ichoran
So you'll need to write a compiler plugin to even get off the ground.
Klas Segeljakt
@segeljakt
I wrote a DSL for building an AST with GADT encoding
Fabio Labella
@SystemFw
or macros, no?
I think it really depends on what you mean by dsl
Ichoran
@Ichoran
Rust's if p { bar(); } else { foo(); } just won't work in Scala.
Fabio Labella
@SystemFw
rust"""
rust code
"""
could be an option
Ichoran
@Ichoran
You could build a Scala DSL to generate Rust MIR, I'm sure. Or a Rust DSL to generate Scala TASTY (from Dotty).
there is also IntelliJ language injection if you have heard about it
// language=Rustval code = “”"
rust code
“”"
Ichoran
@Ichoran
I would think that would be waaaay more relevant for embedding Rust in Haskell?
Klas Segeljakt
@segeljakt
the language injection has some static checks, but only up until name-resolution it seems like
Fabio Labella
@SystemFw
@ichoran I mean, it's not like you are going to find a million blog posts that are related... you'll have to make do with that :P
Ichoran
@Ichoran
Honestly I'm rather puzzled by why this is an interesting thing to do. Rust as a DSL hardly makes sense, since aside from its superior ability to infer types, and its lifetime/ownership system, its syntactic capabilities are basically a subset of Scala's.
Usually you want DSLs because they can express something that the original language can't easily express.