C#에서 string 형식이 primitive일까요?
그동안 별 의심 없이 primitive 타입이라고 생각했는데 마침 책에 대한 다음의 의견이 있어서,
몇 가지 오류
; https://www.sysnet.pe.kr/3/0/3612
다시 정리하게 되었습니다. 질문자의 경우 "레퍼런스 타입인 string 타입은 거기서 제외됩니다."라고 하는 의견을 보내주었는데요.
그렇다면, 우선 primitive 타입의 정의를 먼저 봐야 할 것 같습니다. Wikipedia 정의가 좋겠지요? ^^ (위의 질문자가 "Primitive data type"이라는 복합어가 존재하지 않는다고 하는데... 아쉽지만 이렇게 있습니다.)
Primitive data type
; https://en.wikipedia.org/wiki/Primitive_data_type
- a basic type is a data type provided by a programming language as a basic building block. Most languages allow more complicated composite types to be recursively constructed starting from basic types.
- a built-in type is a data type for which the programming language provides built-in support.
위의 2가지 정의에 따라 비춰보면, string 타입은 C#에게 있어 "primitive" 타입이 맞습니다.
그런데, 재미있는 것은 마이크로소프트의 입장입니다. 현재 MSDN에서 primitive 타입과 닷넷 언어간에 정리된 자료로 다음의 것이 있습니다.
7.3 Primitive Types
- Visual Studio 2003 Retired Technical documentation
; https://msdn.microsoft.com/en-us/library/aa711900(v=vs.71).aspx
; https://www.microsoft.com/en-us/download/details.aspx?id=55979
명시된 것처럼 "string" 타입을 primitive로 분류하고 있지만, 아쉽게도 "Visual Studio .NET 2003 시절의 VB.NET"에 대한 것일 뿐 C#은 아닙니다. 또한, C#의 primitive 타입에 대해 "Eric Lippert"는 2010년 Q&A에서 다음과 같은 의견을 피력하고 있습니다.
In C# are the terms “Primitive” and “Literal” interchangeable?
; http://stackoverflow.com/questions/2066035/in-c-sharp-are-the-terms-primitive-and-literal-interchangeable
...[생략]...
The C# language specification uses the word "primitive" twice; it is never defined and completely vague as to what it could possibly mean.
The C# language spec has no need to use or define the word "primitive" and therefore should not make use of this vague term. I've had a talk with Mads and we've agreed that future editions of the spec will be reworded to eliminate this usage completely.
How other type systems specifications -- the reflection library, the CLI, the VES, and so on -- define the word "primitive" is of course up to them.
Thanks for bringing up the question.
이야기대로, C# 5.0 스펙 문서에 보면 "primitive"라는 단어가 쓰인 것은 단 2번이고, 그나마도 별다른 정의를 하지 않고 쓰여진데다 어떤 타입이 속한다는 것에 관한 분류도 하지 않고 있습니다.
그 외에는 C#의 primitive 타입에 대해 더 이상 어떤 명확한 것을 찾을 수 없었습니다. 이 쯤에서 한번 추측해봐야 할 것 같습니다.
일단, .NET Framework 차원에서는 string 타입이 primitive가 아닙니다. 이는 다음과 같이 테스트 해볼 수 있습니다.
Type type = typeof(string);
Console.WriteLine(type.IsPrimitive); // 출력 결과: False
하지만, "primitive 타입"이란 Framework 차원에서 결정되는 것이 아니고 "프로그래밍 언어" 수준에서 결정되는 것입니다. 만약, 이것이 Framework 차원에서 결정되는 것이라면 ".NET 호환 언어"에서는 아무리 언어 스펙이 "primitive"로 지원하고 있다 해도 결국 그것이 아닌 걸로 결정되는 것입니다.
한 예로 F#의 스펙을 보면,
Primitive Types (F#)
; https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/basic-types
string을 VB.NET과 마찬가지로 Basic types(a superset of .NET primitive types)로 분류하고 있습니다. 그런데 또 재미있는 것은 bigint에 대한 취급입니다. bigint가 System.Numerics.BigInteger에 대한 단순히 약식 표기에 불과하다며 primitive가 아니라는 입장입니다.
모호한 것은 decimal에도 적용됩니다. decimal 역시 .NET Framework 차원에서는 primitive가 아닙니다.
Type type = typeof(decimal);
Console.WriteLine(type.IsPrimitive); // 출력 결과: False
하지만, VB.NET, F#은 물론이고 C#에서 decimal이 primitive 타입이라는 것에는 이견이 없을 것입니다.
즉, 마이크로소프트 내부에서도 이처럼 다양하게 의견이 갈려 있는 것입니다.
휴~~~ 결론 내리기가 쉽지 않습니다.
그래도, 일단 마이크로소프트의 입장을 우선 이해하는 것이 좋을 듯 합니다. Type.IsPrimitive로 인해 언어 상에서의 정의가 매우 애매해졌는데요. 어쨌든 Eric의 의견으로 보면 C#에서만큼은 primitive 단어를 밀고 있지는 않는 것 같습니다. (아마도 그것을 밀자니 프레임워크와의 개념 충돌로 2번 설명해야 하는 어려움이 있을 것입니다.)
실제로 C# 관련 MSDN 문서를 보면 primitive 대신 "Built-In Types"라고 명시하고 있습니다.
Built-In Types Table (C# Reference)
; https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types
따라서, 저는 C#에서의 string이 primitive라고 밀고 싶지만 질문자의 의견에 따라 "Built-in Type"으로 바꾸는 것이 더 나아 보입니다. ^^
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]