Type of "string"

68 views
Skip to first unread message

Stepan Koltsov

unread,
Aug 12, 2012, 8:12:36 AM8/12/12
to clay-l...@googlegroups.com
Hi.

I've fixed a bug in parser combinators https://github.com/jckarter/clay/pull/326 , but now I think that bug is not in parser combinators library, but in compiler. What should be a type of

```
"string"
```

?

Currently type of this expression is Static["string"]. However, type of

```
true
```

is not Static[true], it is Bool. Code:

```
import printer.*;
import printer.formatter.*;

main() {
    ..for (v in true, #true, 1, #1, "st", #"st") {
        println(repr(v), " ", Type(v));
    }
}
```

outputs:

```
true Bool
true Static[true]
1i Int32
1 Static[1]
"st" Static["st"]
"st" Static["st"]
```

There's asymmetry between (true, #true), (1, #1) and ("st", #st).

In clay 0.1 type of "st" is StringConstant. Seems like type of "st" should be StringConstant, but I haven't found such type in master.

--
Stepan Koltsov

Joe Groff

unread,
Aug 13, 2012, 10:57:37 AM8/13/12
to clay-l...@googlegroups.com
On Sun, Aug 12, 2012 at 8:12 AM, Stepan Koltsov
<stepan....@gmail.com> wrote:
> Hi.
>
> I've fixed a bug in parser combinators
> https://github.com/jckarter/clay/pull/326 , but now I think that bug is not
> in parser combinators library, but in compiler. What should be a type of
>
> ```
> "string"
> ```
>
> ?
>
> Currently type of this expression is Static["string"]. However, type of
>
> ```
> true
> ```
>
> is not Static[true], it is Bool. Code:

This is correct. In 0.2 string literals behave like #"identifiers" did
in 0.1. The fact that symbols and string literals are always static
with or without the static operator, while integral constants require
# to be given unique types is an ugly wart, but it requires some elbow
grease to fix. See issue #78 for some discussion:
https://github.com/jckarter/clay/issues/78 Ideally `Static[1]` would
be unnecessary and the compiler would propagate static-ness
automatically without the Static[T] hack.

In 0.2, StringConstant is called StringLiteralRef.
StringLiteralRef("string literal") will give you a StringLiteralRef.

-Joe

Stepan Koltsov

unread,
Aug 13, 2012, 11:17:57 AM8/13/12
to clay-l...@googlegroups.com


Problem with string literals static by default is that Type("aaa") != Type("bbb"). And this causes weird problems like what I fixed in parser combinators. Problems like this:

```
makeFactory(a) = () => a;

main() {
  var g = makeFactory(1);
  g = makeFactory(2);

  var f = makeFactory("aaa");
  f = makeFactory("bbb"); // compile error
}
```

Maybe Clay should change type if "sss" to StringLiteralRef (and #"sss" to Static["sss"]) until https://github.com/jckarter/clay/issues/78 is imlpemented?

--
Stepa

Joe Groff

unread,
Aug 17, 2012, 4:58:52 PM8/17/12
to clay-l...@googlegroups.com
Having string literals be static singletons is a bit of an experiment.
It works well for a lot of things, but it does have some annoying
warts, such as this issue. A new feature in 0.2 is that custom string
literals can be expressed as `foo"bar"`, which is sugar for
`foo("bar")`. If you import StringLiteralRef with a short name, then
runtime string literals can be succintly instantiated with something
like `alias L = StringLiteralRef; var literal = L"literal";`.

-Joe
Reply all
Reply to author
Forward
0 new messages