Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 17, 2021 Why Throwable.message is not a property | ||||
---|---|---|---|---|
| ||||
The question is why Throwable.message is not a @property?! It looks strange now, because "message" is not a *verb*, but a *noun*. So it's expected to be a property. Also because it is not a property in some contexts when I try to concatenate it with string without parentheses using "~" operator it fails, because (as you could expect) it is a *regular* function, but not a property. I wonder if it was made as *non-property* by some reason or by oversight? Thanks |
March 17, 2021 Re: Why Throwable.message is not a property | ||||
---|---|---|---|---|
| ||||
Posted in reply to uranuz | On Wednesday, 17 March 2021 at 17:46:27 UTC, uranuz wrote:
> Also because it is not a property in some contexts when I try to concatenate it with string without parentheses using "~" operator it fails
Can you post some sample code that demonstrates this?
|
March 17, 2021 Re: Why Throwable.message is not a property | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 17 March 2021 at 17:52:20 UTC, Adam D. Ruppe wrote:
> On Wednesday, 17 March 2021 at 17:46:27 UTC, uranuz wrote:
>> Also because it is not a property in some contexts when I try to concatenate it with string without parentheses using "~" operator it fails
>
> Can you post some sample code that demonstrates this?
Seems that a problem with concatenation is because Throwable.message has const(char)[] type, but not string. This makes some inconvenience ;-)
There is an example:
import std;
void main()
{
auto exc = new Exception("Test");
string longMsg = "The: " ~ exc.message; // Adding parentheses () after "message" actually doesn't change anything. Error is the same
writeln(longMsg);
}
Compile error:
onlineapp.d(6): Error: cannot implicitly convert expression "The: " ~ exc.message() of type char[] to string
I could add cast(string), but it's not something I want to do.
The reason, why I want to use "message" instead of "msg" is that I want to add some extra information to exception as separate typed fields. But I want it to be displayed when converting exception to string. So I shall override "message" and convert this extra info to string...
|
March 17, 2021 Re: Why Throwable.message is not a property | ||||
---|---|---|---|---|
| ||||
Posted in reply to uranuz | On Wednesday, 17 March 2021 at 19:32:02 UTC, uranuz wrote:
> Seems that a problem with concatenation is because Throwable.message has const(char)[] type, but not string. This makes some inconvenience ;-)
Yes, that's what I thought.
The concat operation tends to give the most flexible type of the arguments... and I wish it would then ACTUALLY use that flexibility... but it doesn't.
Regardless though since you know you are concating it, which means you get a new string anyway, you can safely cast(string) it.
string longMsg = "The: " ~ cast(string) exc.message;
that's how i do it.
|
March 17, 2021 Re: Why Throwable.message is not a property | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 17 March 2021 at 19:38:48 UTC, Adam D. Ruppe wrote:
> On Wednesday, 17 March 2021 at 19:32:02 UTC, uranuz wrote:
>> Seems that a problem with concatenation is because Throwable.message has const(char)[] type, but not string. This makes some inconvenience ;-)
>
> Yes, that's what I thought.
>
> The concat operation tends to give the most flexible type of the arguments... and I wish it would then ACTUALLY use that flexibility... but it doesn't.
>
> Regardless though since you know you are concating it, which means you get a new string anyway, you can safely cast(string) it.
>
> string longMsg = "The: " ~ cast(string) exc.message;
>
> that's how i do it.
This is what I have done ;-)
|
Copyright © 1999-2021 by the D Language Foundation