Comments (3)
class Foo
var x: U32
fun ref foo(y: U32, z: U32): U32 => x + y + z
fun ref test(): U32 =>
var f = this!foo(2)
f(3)
// rewrites to:
var f = {
var this' = this
var y: U32 = 2
fun ref apply(z: U32): U32 => this'.foo(y, z)
}
f.apply(3)
Doing this with just traits would be problematic. What would be the type of f
?
from ponyc.
The example I gave in issue #10 seems to require structural types. I'm pasting the example here, for convenience.
In pony, types with different type parameters don't have a subtype relationship. So, given:
trait Animal
class Wombat is Animal
Array[Animal]
and Array[Wombat]
are not related types. This makes them type safe, unlike Java, but since we don't have per-type parameter variance annotation, if we stopped here we would be missing out on some things, such as passing an Array[Wombat]
where an Array[Animal]
is expected if we know they will be read, but not written (which would be type safe).
However, with contravariant parameters and covariant results, we can. Here, I craft two type aliases for structural types that allow me to read elements from anything that looks like a sequence:
interface SequenceReader[A]
fun box apply(i: U64): this->A
interface SequenceWriter[A]
fun ref update(i: U64, v: A)
Now, Array[Wombat]
is a subtype of SequenceReader[Animal]
, without being a subtype of Array[Animal]
, because fun box apply(i: U64): this->Wombat
is a subtype of fun box apply(i: U64): Animal
since the parameter types are contravariant (in this case, equal) and the return type is covariant.
Similarly, Array[Animal]
is a subtype of SequenceReader[Wombat]
without being a subtype of Array[Wombat]
, because fun box update(i: U64, v: Animal)
is a subtype of fun box update(i: U64, v: Wombat)
since the parameter types are contravariant and the return type is covariant (in this case, equal).
So trait method parameter contravariance and return type covariance gives us all the power of a subtype relationship between types with different formal parameters without the type safety problem (Java) or complex annotation (Scala: http://blogs.atlassian.com/2013/01/covariance-and-contravariance-in-scala/ ).
from ponyc.
After much discussion and consultation, we determined:
- Structural types are both necessary and preferred. However, the syntax was wrong, as it wasn't possible to have parameterised structural types. We have got rid of the
{...}
syntax, and instead we'll use the keywordinterface
for a structural type, defining them otherwise just like a trait, including being able to provide default method implementations that are picked up only if a type puts the interface in it's "provides" list. - Nominal types are still necessary. The particular motivating example is pattern matching on type for types that come from different packages but may be otherwise identically named (such as needing to use two versions of the same library in the same program). There are other useful ways to use nominal types, but this example is impossible with just structural types.
This approach will also eventually allow pattern matching on interfaces (ie structural types) as well as traits (ie nominal types).
from ponyc.
Related Issues (20)
- Feat: Reconsider support for ponyc bundled builds targeting Intel MacOS HOT 1
- runaway memory growth during GC with reference cycle HOT 12
- RFC #77: Assign Param Syntax
- RFC #78: Remove JSON package from the standard library
- Can't built 0.53.0 on aarch64-darwin HOT 8
- Possible violation of the `pthread_create` interface? HOT 4
- Release 0.54.1 HOT 2
- iftype compile error at runtime HOT 7
- [Epic] EL 9 & Fedora support - prebuilt, install.md, and ponyup HOT 1
- XorOshiro naming in random HOT 2
- Incorrect program result when not using --debug HOT 16
- Release 0.55.0
- Segmentation fault when capturing Env via lambda HOT 17
- Segfault when trying to compile incorrect capabilities HOT 12
- With block doesn't call dispose on an object if its name is _ HOT 7
- Web assembly
- Unreported type inference error with Array's in match case statements HOT 9
- Cannot build with --static nor --nopic. HOT 7
- Release 0.55.1 HOT 1
- How to cross compile for armv7? HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ponyc.