# Java-style Object field

**URL:** <https://forum.crystal-lang.org/t/java-style-object-field/1064>\
**Category:** Help & Support\
**Created:** [August 24, 2019, 1:53am UTC](https://forum.crystal-lang.org/t/java-style-object-field/1064 "2019-08-24T01:53:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwagner](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/iwagner/32/523_2.png) [@iwagner](https://forum.crystal-lang.org/u/iwagner)\
**Post date:** [August 24, 2019, 1:53am UTC](https://forum.crystal-lang.org/t/java-style-object-field/1064/1 "2019-08-24T01:53:36Z")

</div>

Hello. I’m trying to implement a simple AST interpreter in Crystal. I need a Token class. My current attempt at writing it looks like this:

```
class Token
  getter type : TokenType
  getter literal
  getter lexeme : String
  getter line : Int32

  def initialize(@type, @literal, @lexeme, @line)
  end
end

```

The problem is that I don’t know what type I should use for the _literal_ field. In theory, it should be possible to keep values of different types in it. In Java, there’s the _Object_ class that can be used as an universal wrapper for instances of any classes. But in Crystal, _Object_ works in a different way (if I understood it correctly).

So, what should I use instead?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [August 24, 2019, 1:58am UTC](https://forum.crystal-lang.org/t/java-style-object-field/1064/2 "2019-08-24T01:58:05Z")

</div>

Generics would be good for this.

[https://crystal-lang.org/reference/syntax\_and\_semantics/generics.html](https://crystal-lang.org/reference/syntax_and_semantics/generics.html)

If all the possible tokens are known, you could also use an enum. See [https://crystal-lang.org/api/master/JSON/Token/Kind.html](https://crystal-lang.org/api/master/JSON/Token/Kind.html)

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [August 24, 2019, 2:13am UTC](https://forum.crystal-lang.org/t/java-style-object-field/1064/4 "2019-08-24T02:13:01Z")

</div>

It’s common for tokens to contain properties of each type they can represent. For example:

```crystal
class Token
  enum Type
    Bool
    String
    Int
    Float
  end

  property type : Type
  property string_value : String = ""
  property int_value : Int::Primitive = 0
  property float_value : Float::Primitive = 0.0
  property bool_value : Bool

  def value : Value
    case type
    when .float?
      float_value
    when .int?
      int_value
    when .bool?
      bool_value
    when .string?
      string_value
    end
  end
end

alias Value =
  Bool |
  Int::Primitive |
  Float::Primitive |
  String

```

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [August 24, 2019, 9:07am UTC](https://forum.crystal-lang.org/t/java-style-object-field/1064/5 "2019-08-24T09:07:13Z")

</div>

As the others here outlined, you basically need to list out all the possible types. Given literals, these shouldn’t be too many. The most simple solution could be an alias like `alias Literal = Int|Float|String|Bool`

---

<div class="post-metadata">

**Author:** ![stronny](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/stronny/32/228_2.png) [@stronny](https://forum.crystal-lang.org/u/stronny)\
**Post date:** [August 26, 2019, 7:01pm UTC](https://forum.crystal-lang.org/t/java-style-object-field/1064/6 "2019-08-26T19:01:34Z")

</div>

Why don’t you just use the class hierarchy directly?

```auto
abstract class Token
  getter line : Int32
  getter lexeme : String
  def initialize(@lexeme, @line); end
end

class Token::Number < Token
  getter literal : Int32
  def initialize(@literal, *args)
    super *args
  end
end

class Token::String < Token
  getter literal : String
  def initialize(@literal, *args)
    super *args
  end
end

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [September 4, 2019, 8:01pm UTC](https://forum.crystal-lang.org/t/java-style-object-field/1064/7 "2019-09-04T20:01:09Z")

</div>

In general: It would be great if Crystal’s `Object` type (or at least `Reference`) could be used for instance variables. Right now it doesn’t, but it might some day. You could hack your way around it by including a module into `Object` and using the module instead. But that stinks and breaks easily.

For your specific example, I second @jhass that you can probably just use a union of a few types that could be used to represent literal values. There should just be a hand full.

Using subclasses as suggested by @stronny looks way more complex than necessary. An easier alternative would be a generic type `Token(T)`, but even that is probably too much and not necessary.
