Why is 'Character#' not a valid mathlab variable name?
古いコメントを表示
Why is 'Character#' not a valid MATLAB variable name?
回答 (3 件)
DGM
2023 年 2 月 17 日
0 投票
"A valid variable name starts with a letter, followed by letters, digits, or underscores."
2 件のコメント
Walter Roberson
2023 年 2 月 17 日
But that doesn't explain why ... ;-)
DGM
2023 年 2 月 18 日
According to inside sources, it's because a very tall hamster wanted it that way.
Walter Roberson
2023 年 2 月 17 日
編集済み: Walter Roberson
2023 年 2 月 18 日
0 投票
I looked up several different lists of the most popular programming languages, and checked reference documentation for each .
# is not a valid character in variable names in:
- Ada 95 (but might be possible in Ada 2003)
- Algol
- C
- C++
- C#
- fortran
- Go
- HCL (permits $ but not as first character)
- java
- javascript
- Kotlin
- Lua
- Makefile (‘:’, ‘#’, ‘=’, and whitespace are specifically prohibitted in variable names)
- pascal
- php
- python
- R (permits periods in identifiers)
- Ruby
- Shell
- Swift (permits $ and a number of Unicode characters in variable names, but not # )
- typescript (permits $ including as first character)
Ones that do permit #:
- Ada 2003 (permits many unicode characters)
- Common Lisp (requires special syntax) (permits many unicode characters)
- Maple (requires special syntax) (permits many unicode characters)
- Mathematica (permits many unicode characters)
- Rust (permits many unicode characters)
it is difficult to answer exactly "why" any of these languages made the choices they did.
I suspect in the case of MATLAB it is because the permitted identifiers are the same as the identifier forms permitted by the linkers on the operating systems of implementation. (In some cases the $ is also permitted by the linkers, but it tends to have special meaning when it is permitted.)
John D'Errico
2023 年 2 月 18 日
編集済み: John D'Errico
2023 年 2 月 19 日
MATLAB itself can tell you. Just ask.
why
why
why
why
why
Do you want a better reason? Just because. Seriously, does there need to be a better reason? Sometimes you just need to accept something as it is.
You should understand that special characters in names often may create subtle problems. For example, why would MATLAB not allow you to start a variable name with a number? That is, why can x5 be a variable name, but not 5x? This may get into parser issues, where by removing the possibility of a number at the beginning of the name, it made the parser code far easier to write. Eliminating that possibility reduces the chances for bugs in your own code too.
But special characters often have specific uses. For example, the @ symbol has specific uses in MATLAB. They could not allow you to use it. As well symbols like ~ also have their own uses. And who knows when next year, they might find a real need for some other special character as a new operator of some sort? This means the language would need to exclude certain specific special characters from use in variable names. So instead of giving the user a list of which special chacracters you could use, it is far easier and better to just declare ALL special characters as illegal in a variable name. That makes the rules for a variable name quite simple. And in turn, that is a good thing.
By the way, it is MATLAB, not mathlab.
1 件のコメント
Steven has a comment that I think is a good addition:
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!