-
-
Notifications
You must be signed in to change notification settings - Fork 606
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
Issue 481 - Letting compiler determine length for fixed-length arrays #3615
Conversation
TemplateInstance *ti = se->sds->isTemplateInstance(); | ||
if (ti && ti->semanticRun == PASSsemantic && !ti->aliasdecl) | ||
se->error("cannot infer type from %s %s, possible circular dependency", se->sds->kind(), se->toChars()); | ||
else | ||
se->error("cannot infer type from %s %s", se->sds->kind(), se->toChars()); | ||
return Type::terror; | ||
goto Lerror; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer you just returned new ErrorInitializer();
everywhere here instead of using goto.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
This looks like a small but nice improvement for the D language. I hope to see this merged in dmd 2.066.
|
Nice work, I'm glad to see you found the time to work on it. |
@bearophile To me supporting deduction in cast-expression is subtle feature. Rather than that, we need to think decreasing use of casts. |
Spec change: dlang/dlang.org#590 |
@9rnsr: I am not asking for that feature. I was just curious to know if that works or not. And I agree with decreasing the usage of casts as much as possible. The recent double(x) syntax has allowed me to remove several casts. |
This PR does not support |
After the merge of this patch a common problem left in D code is:
The current solution is long, requires a cast and is not fully DRY (because the 'x' name is repeated twice):
Because we don't have a 'mutable' keyword:
|
Actually it's really really easy: void foo(in int x) {
int y = x;
y++;
} or void foo(int x) {
auto y = x;
y++;
} |
@yebblies: yes I considered those possibilities, but the first is not DRY for the type of x (but I sometimes use it). The second is worse because it leaves x mutable, that is bug-prone. |
Thanks, Kenji, for doing this. My reservation on this is I keep thinking there must be a better way than [$]. |
Please, let's keep PR discussions on topic. Other issues belong in bugzilla or the n.g. |
@WalterBright > Please, let's keep PR discussions on topic. This nice enhancement allows D partial inference, allowing the programmer to omit useless information, and to specify what's important for the program:
With "immutable[$]" you are specifying you want an immutable fixed-size array, that you want the compiler to infer its length, but you don't want to specify its item type. So you are omitting part of the information, and you are changing part of the information in a way different from the standard (because by default if you use auto you get a dynamic array, not a fixed size one). So while the following problem is not covered by this enhancement, I don't agree it's off topic:
It's a related problem, it's still a case where you want to partially specify a type, so you want to omit the type of the variable y but you want to change part of its type compared to what it gets by default if you use auto (auto makes it const, as the variable x). So using the same syntax as the enhancement request you get a syntax like this:
Currently in D there is no mutable keyword, so that's impossible. But when you design a new feature, like this partial inference, it's important to look at the wider picture. |
@WalterBright: >My reservation on this is I keep thinking there must be a better way than [$]. This is an old enhancement request, several people have discussed and given their mind to this topic, so far I have not seen better ideas. The $ symbol is already used to denote length so I think the syntax proposed here is sufficiently intuitive for D programmers. And there's need for this enhancement, now even more than before because @nogc allows us to spot the unnecessary heap allocations. The [$] syntax helps remove some heap allocations and at the same time to keep the code easy to write and not bug prone (because the programmer doesn't need to count the items in the array literal, so the code is more DRY). Regarding this enhancement patch, as you see it's not just about [$]. It adds some more value on the table, like "immutable[]" and "auto*" that while not as useful as the [$] syntax, will make D code a little nicer. |
Just a thought: wouldn't having static array literals (discussed in the links below) solve the problem for rvalues as well as lvalues, while this approach only solves the problem for lvalues? Partially-typed variable declaration:
Static array literals:
Relevant links: |
@MasonMcGill I think "static array literal syntax" is a bad idea. |
@WalterBright Is your objection just to the syntax
|
@quickfur That looks a lot like an associative array declaration, but I guess you can tell that it's not from the right hand side. |
Hmm, good point. It could be construed to mean "automatically infer key type". :-( |
Support partial type deduction in AA key. For example: int[auto] aa = ["a":1, "b":2, "c":3]; // typeof(aa) will be int[string] |
@9rnsr Very nice!! I like it very much. Does it also support |
@quickfur Yes. |
Awesome! |
Let's pull this in. I approve, and @WalterBright implicitly approves by liking the feature and not finding a better syntax :). |
Auto-merge toggled on |
Issue 481 - Letting compiler determine length for fixed-length arrays
See https://issues.dlang.org/show_bug.cgi?id=14066 (found about one minute after installing the new patch). |
Is all this correct? I use a const[$] and I receive an immutable(char[5]).
|
Is this a good idea?
|
void main() {
const[$] s1 = "hello";
pragma(msg, typeof(s1)); // immutable(char[5]) it's a bug in partial type deduction implementation. The char[$] s2 = "hello"; // OK?
s2[0]++; // OK?
pragma(msg, typeof(s2)); // char[5] That's intended behavior. The s2 type is deduced to |
Legitimate enhancement. Please file it in bugzilla! |
Great to see this in. |
@9rnsr: Does |
@AndrejMitrovic It works and properly allowed by the grammar. |
@MartinNowak I'll do it in the documentation PR: dlang/dlang.org#590 |
I hate to say this but I just can't bring myself to agree with this feature - [$] but partial deduction, too.
It is my opinion we should retire this feature before 2.067 is released. I am really sorry for not being more decisive on this and left all this work to be done. |
I agree with @andralex |
私はこの変更が戻されようとすることには反対しない。 しかし、私はあなたに怒っている。 あなたの行動はつまり、このPRをマージするときあなたはこの変更について深く考えていなかったことを示している。 従って、私は今あなたのD言語に対する真剣さを疑っている。 あなたがDに対して真剣でないなら、私はあなたの主張全てを受け入れることが出来ない。 このPRをマージするとき、あなたは何を考えていた? |
I'd like to reopen discussion for partial specialization, at the very least.
For the case of "int[$]" this is true, as it would be an entirely new feature and would need to be propagated to places where declarations can occur. For partial specialization, it already exists for function templates. Thus partial specialization already exists in the language, but it only exists in functions. What I'm asking is to extend it to auto declarations for variables and in foreach statements. void func(Auto)(Auto* ptr) // existing behavior
{
}
void func2(Auto)(Auto[string] aa) // existing behavior
{
}
int* p;
func(p);
int[string] aa;
func2(aa);
func(int.init); // err
func2(int.init); // err
|
I don't quite get it, |
That's the point, int.init would just be an int value, which neither function allows do to the partial specialization. What this pull request added was partial specialization to "auto": auto* ptr = null; // ok
auto* ptr = 0; // err What's "ng"? |
NG = Newsgroup. Related Thread is: http://forum.dlang.org/post/bewroetnschakoqjzowa@forum.dlang.org |
Merge `stable` in `mater` Signed-off-by: Iain Buclaw <ibuclaw@users.noreply.github.com> Merged-on-behalf-of: Petar Kirov <PetarKirov@users.noreply.github.com>
https://issues.dlang.org/show_bug.cgi?id=481
Add syntax support for partially typed variable declaration.