-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Same Type As Requirement #19
Comments
@everythingfunctional I don't think you need to be so pessimistic on this front. The requirement your are describing is an important one even without traits. Use cases will matter though. For instance what is it about x, y and z being the same type that is important here? To examine this we need some additional pseudo syntax to relate the trait back to the actual template. (First I had to go find a more complicated Rust example to see what happens when the trait involved additional types.) Consider the following as a generalization of what I understand from your example
The case where all 3 type parameters are the same type is then just a special case. But the new template will support
One of the abstract lessons I have learned with Magne is that one should not be stingy with type parameters. When you think two types "must" be the same, it is really just a relation between them that is screaming to get out. (E.g., the ability to add in this case.) I envision TYPEOF and CLASSOF will become relatively rare in advanced templates. But still absolutely necessary on occasion. PS the new |
This is a bit tangential, but I thought some more about this issue. In the add example, there are two possibilities. The first is that this is a type-bound procedure. The second is that this is a regular procedure. Unfortunately with Fortran, both cases have the same interface and the difference is in how the interface is accessed. So is the specified trait a promise that the type has a type-bound procedure named "add" , or that there is a publicly accessible procedure of that name Unfortunately, I think we really need to support both cases, and therefore will need to invent some sort of notation to make the distinction. |
Having special trait syntax to ensure that two or more dummy arguments are the same type seems a bit overkill. I would think that declaring the dummy arguments (and/or function result) with the same template type parameter will be a more common use case (and it will ensure that the two objects are the same type). For example, function add < T > (x, y) result(z) But there might be a legitimate concern on T being a valid type for add(). And what constitutes a valid type could be spelled out in a "trait" syntax. Edit: Had to add some space to < T > to get it to show up in the saved message. |
I thought the idea was to avoid discussing syntax at this stage. In any case it is ambiguous where you want to define the "trait", in the generic or in the context of instantiation. I can see two ways of defining the "trait" in the generic. In Fortranesque pseudocode as a type attribute
or as a separate procendure
If you really want to use typeof
|
@wclodius2 Yes - the approach you suggest is pretty much what Magne and I had been considering in our discussions. |
One thing that seems to come up frequently in generics (especially with what seems to be the quintessential example for Fortran), is specifying a "Trait" (or a "template" procedure) in such a way as two (or more) arguments must have the same type. For example, if I want to define a trait that requires the
add
function, in (psuedo)code, I'd like to have it look something like the following:but (AFAIK),
typeof
only works for variables with a declared, specific type. I think it would make sense to have it work in this context, but it's something we should be thinking about.The text was updated successfully, but these errors were encountered: