nellyjelly56
nellyjelly56

Reputation: 13

How can I assign user defined sml datatypes to have a integer (as string) datatype?

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

Upvotes: 0

Views: 83

Answers (1)

Chris
Chris

Reputation: 36581

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.

Upvotes: 1

Related Questions