11/*
2- * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
2+ * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1818import java .io .Serial ;
1919import java .io .Serializable ;
2020import java .util .*;
21+ import java .util .stream .Collectors ;
2122
2223/** A Pkl type as used in type annotations. */
2324public abstract class PType implements Serializable {
@@ -27,18 +28,33 @@ public abstract class PType implements Serializable {
2728 public static final PType UNKNOWN =
2829 new PType () {
2930 @ Serial private static final long serialVersionUID = 0L ;
31+
32+ @ Override
33+ public String toString () {
34+ return "unknown" ;
35+ }
3036 };
3137
3238 /** The bottom type. */
3339 public static final PType NOTHING =
3440 new PType () {
3541 @ Serial private static final long serialVersionUID = 0L ;
42+
43+ @ Override
44+ public String toString () {
45+ return "nothing" ;
46+ }
3647 };
3748
3849 /** The type of the enclosing module. */
3950 public static final PType MODULE =
4051 new PType () {
4152 @ Serial private static final long serialVersionUID = 0L ;
53+
54+ @ Override
55+ public String toString () {
56+ return "module" ;
57+ }
4258 };
4359
4460 private PType () {}
@@ -59,6 +75,10 @@ public StringLiteral(String literal) {
5975 public String getLiteral () {
6076 return literal ;
6177 }
78+
79+ public String toString () {
80+ return ValueFormatter .basic ().formatStringValue (literal , "" );
81+ }
6282 }
6383
6484 public static final class Class extends PType {
@@ -92,6 +112,18 @@ public PClass getPClass() {
92112 public List <PType > getTypeArguments () {
93113 return typeArguments ;
94114 }
115+
116+ @ Override
117+ public String toString () {
118+ var result = pClass .getDisplayName ();
119+ if (!typeArguments .isEmpty ()) {
120+ result +=
121+ "<"
122+ + typeArguments .stream ().map (Object ::toString ).collect (Collectors .joining (", " ))
123+ + ">" ;
124+ }
125+ return result ;
126+ }
95127 }
96128
97129 public static final class Nullable extends PType {
@@ -106,6 +138,13 @@ public Nullable(PType baseType) {
106138 public PType getBaseType () {
107139 return baseType ;
108140 }
141+
142+ @ Override
143+ public String toString () {
144+ return baseType instanceof Function || baseType instanceof Union
145+ ? "(" + baseType + ")?"
146+ : baseType + "?" ;
147+ }
109148 }
110149
111150 public static final class Constrained extends PType {
@@ -126,6 +165,16 @@ public PType getBaseType() {
126165 public List <String > getConstraints () {
127166 return constraints ;
128167 }
168+
169+ @ Override
170+ public String toString () {
171+ return (baseType instanceof Function || baseType instanceof Union
172+ ? "(" + baseType + ")"
173+ : baseType )
174+ + "("
175+ + String .join (", " , constraints )
176+ + ")" ;
177+ }
129178 }
130179
131180 public static final class Alias extends PType {
@@ -161,6 +210,18 @@ public List<PType> getTypeArguments() {
161210 public PType getAliasedType () {
162211 return aliasedType ;
163212 }
213+
214+ @ Override
215+ public String toString () {
216+ var result = typeAlias .getDisplayName ();
217+ if (!typeArguments .isEmpty ()) {
218+ result +=
219+ "<"
220+ + typeArguments .stream ().map (Object ::toString ).collect (Collectors .joining (", " ))
221+ + ">" ;
222+ }
223+ return result ;
224+ }
164225 }
165226
166227 public static final class Function extends PType {
@@ -181,6 +242,14 @@ public List<PType> getParameterTypes() {
181242 public PType getReturnType () {
182243 return returnType ;
183244 }
245+
246+ @ Override
247+ public String toString () {
248+ return "("
249+ + parameterTypes .stream ().map (Object ::toString ).collect (Collectors .joining (", " ))
250+ + ") -> "
251+ + returnType ;
252+ }
184253 }
185254
186255 public static final class Union extends PType {
@@ -195,6 +264,11 @@ public Union(List<PType> elementTypes) {
195264 public List <PType > getElementTypes () {
196265 return elementTypes ;
197266 }
267+
268+ @ Override
269+ public String toString () {
270+ return elementTypes .stream ().map (Object ::toString ).collect (Collectors .joining (" | " ));
271+ }
198272 }
199273
200274 public static final class TypeVariable extends PType {
@@ -213,5 +287,10 @@ public String getName() {
213287 public TypeParameter getTypeParameter () {
214288 return typeParameter ;
215289 }
290+
291+ @ Override
292+ public String toString () {
293+ return typeParameter .getName ();
294+ }
216295 }
217296}
0 commit comments