tov / dssl2

A data structures student language, version 2
MIT License
9 stars 5 forks source link

Inline Struct Literals #23

Closed markovejnovic closed 1 year ago

markovejnovic commented 4 years ago

The language does not appear to support inline struct literal construction syntax.

This statement does not work:

let customers = [
    customer { name: 'Alice', bank_account: Account(1, 'savings', 10) },
    customer { name: 'Bob', bank_account: Account(2, 'checking', 5) }
]

The following error is thrown:

hw1.rkt:163:8: Syntax error: unexpected token ‘customer’
  context...:
   /usr/share/racket/collects/syntax/readerr.rkt:6:2: raise-read-error
   /usr/share/racket/pkgs/parser-tools-lib/parser-tools/yacc.rkt:347:16: parsing-loop
   /usr/share/racket/collects/syntax/module-reader.rkt:186:17: body
   /usr/share/racket/collects/syntax/module-reader.rkt:183:2: wrap-internal
   lang:read-syntax
   read-syntax
   default-load-handler
   standard-module-name-resolver
   module-path-index-resolve
   [repeats 1 more time]
   module-declared?

However, this syntax works, throwing no errors:

let c1 = customer { name: 'Alice', bank_account: Account(1, 'savings', 10) }
let c2 = customer { name: 'Bob', bank_account: Account(2, 'checking', 5) }

let customers = [c1, c2]

Also, please observe that the following will throw an error:

let customers = [
    { name: 'Alice', bank_account: Account(1, 'savings', 10) },
    { name: 'Bob', bank_account: Account(2, 'checking', 5) }
]

That error is:

hw1.rkt:155:8: Syntax error: unexpected token ‘LBRACE’
  context...:
   /usr/share/racket/collects/syntax/readerr.rkt:6:2: raise-read-error
   /usr/share/racket/pkgs/parser-tools-lib/parser-tools/yacc.rkt:347:16: parsing-loop
   /usr/share/racket/collects/syntax/module-reader.rkt:186:17: body
   /usr/share/racket/collects/syntax/module-reader.rkt:183:2: wrap-internal
   lang:read-syntax
   read-syntax
   default-load-handler
   standard-module-name-resolver
   module-path-index-resolve
   [repeats 1 more time]
   module-declared?

This appears to indicate that inline struct literal construction does not work. Note that class construction is also employed in this example and it does not present any issues to the interpreter.

Perhaps it would be beneficial and would be a QOL upgrade to enable inline struct construction.

Additional Information

tov commented 4 years ago

Thanks! Wonder what this is about…

tov commented 4 years ago

I agree it’s a bug, not just QOL. I think the curly brace syntax for struct construction is kind of neglected—I usually don’t use it since it’s more verbose.

tov commented 4 years ago

I can’t seem to reproduce this. In particular, this works for me:

#lang dssl2

struct Account:
    let id
    let type
    let balance

struct customer:
    let name
    let bank_account

let customers = [
    customer { name: 'Alice', bank_account: Account(1, 'savings', 10) },
    customer { name: 'Bob', bank_account: Account(2, 'checking', 5) }
]

In your example is customer by any chance imported from another module? That isn’t supported for… reasons.

tov commented 4 years ago

(That was wrong. It should also work when imported.)

markovejnovic commented 1 year ago

Sorry for not following up on this -- didn't see the notification when this was relevant...

Seems like it's my fault somewhere -- I'd close this as a non-issue.