0

My problem is that I'm trying to make my own datatype, but I can not use ints in the naming convention of the items that are appart of the datatype

datatype psu = 350w | 450w | 550w | ... etc;

error thrown:

stdIn:16.15-16.30 Error: syntax error: deleting  STRING BAR STRING

Is there some way I can convert these numbers into a string that would be acceptable for the sml interpreter? I really don't want to name my variable "three hundred and fifty watts" or anything.

I tried looking up a toString and just making the variables explicitly strings, but I could not find a helpful toString and making the variables strings just threw another error

3
  • 1
    An identifier can't begin with a digit. What's wrong with datatype psu = W350 | W450 | W550 | ..., or datatype psu = Watts of int? What does this type represent and how are you going to use it? Commented Apr 1, 2022 at 6:13
  • BTW: that definition does not produce that error. Your actual definition said datatype psu = "350w" | "450w" | .... I think you've been so focused on strings that you're becoming stuck in them as a solution, but they rarely are. Commented Apr 1, 2022 at 6:21
  • Even I was about to suggest this..datatype psu = W350 | W450 | W550 | ... Commented Apr 1, 2022 at 6:34

1 Answer 1

1

As noted in comments, identifiers in SML (as in many other programming languages) cannot begin with digits.

datatype psu = W350 | W450 | W550 | ...

This would work.

Something like the following would also work, but does not let you constrain the possibilities.

datatype psu = Watts of int

Similarly, if you represent that value as a string like "350W" you cannot ensure at compile-time that the value held in that string is in a particular set of options.

Sign up to request clarification or add additional context in comments.

1 Comment

I did not realize that identifiers in sml could not start with digits. I guess I will use the W350 style for my program. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.