Skip to main content
added 77 characters in body
Source Link
Giorgio
  • 19.8k
  • 16
  • 89
  • 137

I also attended the same Scala course and I agree with e-i-s that you should use the Option[T] type for implementing an optional field. Here are some more ideas.

The pattern involving

  • a trait with method isEmpty
  • several classes or objects extending the trait
  • a unique object whose method isEmpty always returns true

is also used to implement sequence-like immutable structures such as lists, streams, and so on.

Compare:

  1. An Option[T] type containing the values Some(t), for each value t in T, and the additional value None.
  2. A List[T] type containing objects built from two subclasses (and their respective constructors) Cons[T](h: T, t: List[T]) and Nil: these represent immutable lists whose elements have type T. Note that here the method Nil.isEmpty returns true.

None indicates the absence of a value, while Nil indicates an empty list.

So an optional value None is not the same as an empty list Nil.

NOTE

If you need to test if an optional value is defined or not you can use the isEmpty method of the Option type, or pattern matching:

class MyClass(f: Option[String])
{
  val field = f
}

and then, to test if the field of an instance obj of MyClass is defined:

if (obj.field.isEmpty)
{
}
else
{
}

or

obj.field match
{
  case Some(s) => /* do something with s */
  case None    => /* ... */
}

I also attended the same Scala course and I agree with e-i-s that you should use the Option[T] type for implementing an optional field.

The pattern involving

  • a trait with method isEmpty
  • several classes or objects extending the trait
  • a unique object whose method isEmpty always returns true

is used to implement sequence-like immutable structures such as lists, streams, and so on.

Compare:

  1. An Option[T] type containing the values Some(t), for each value t in T, and the additional value None.
  2. A List[T] type containing objects built from two subclasses (and their respective constructors) Cons[T](h: T, t: List[T]) and Nil: these represent immutable lists whose elements have type T. Note that here the method Nil.isEmpty returns true.

None indicates the absence of a value, while Nil indicates an empty list.

So an optional value None is not the same as an empty list Nil.

NOTE

If you need to test if an optional value is defined or not you can use pattern matching:

class MyClass(f: Option[String])
{
  val field = f
}

and then, to test if the field of an instance obj of MyClass is defined:

obj.field match
{
  case Some(s) => /* do something with s */
  case None    => /* ... */
}

I also attended the same Scala course and I agree with e-i-s that you should use the Option[T] type for implementing an optional field. Here are some more ideas.

The pattern involving

  • a trait with method isEmpty
  • several classes or objects extending the trait
  • a unique object whose method isEmpty always returns true

is also used to implement sequence-like immutable structures such as lists, streams, and so on.

Compare:

  1. An Option[T] type containing the values Some(t), for each value t in T, and the additional value None.
  2. A List[T] type containing objects built from two subclasses (and their respective constructors) Cons[T](h: T, t: List[T]) and Nil: these represent immutable lists whose elements have type T. Note that here the method Nil.isEmpty returns true.

None indicates the absence of a value, while Nil indicates an empty list.

So an optional value None is not the same as an empty list Nil.

NOTE

If you need to test if an optional value is defined or not you can use the isEmpty method of the Option type, or pattern matching:

class MyClass(f: Option[String])
{
  val field = f
}

and then, to test if the field of an instance obj of MyClass is defined:

if (obj.field.isEmpty)
{
}
else
{
}

or

obj.field match
{
  case Some(s) => /* do something with s */
  case None    => /* ... */
}
Post Undeleted by Giorgio
Post Deleted by Giorgio
added 24 characters in body
Source Link
Giorgio
  • 19.8k
  • 16
  • 89
  • 137

I also attended the same Scala course and I agree with e-i-s that you should use the Option[T] type for implementing an optional field.

The pattern involving

  • a trait with method isEmpty
  • several classes or objects extending the trait
  • a unique object whose method isEmpty always returns true

is used to implement sequence-like immutable structures such as lists, streams, and so on.

Compare:

  1. An Option[T] type containing the values Some(t), for each value t in T, and the additional value None.
  2. A List[T] type containing objects built from two subclasses (and their respective constructors) Cons[T](h: T, t: List[T]) and Nil: these represent immutable lists whose elements have type T. Note that here the method Nil.isEmpty returns false as you mentionedtrue.

None indicates the absence of a value, while Nil indicates an empty list.

So an optional value None is not the same as an empty list Nil.

NOTE

If you need to test if an optional value is defined or not you can use pattern matching:

class MyClass(f: Option[String])
{
  val field = f
}

and then, to test if the field of an instance obj of MyClass is defined:

obj.field match
{
  case Some(s) => /* do something with s */
  case None    => /* ... */
}

I also attended the same Scala course and I agree with e-i-s that you should use the Option[T] type for implementing an optional field.

The pattern involving

  • a trait with method isEmpty
  • several classes or objects extending the trait
  • a unique object whose method isEmpty always returns true

is used to implement sequence-like immutable structures such as lists, streams, and so on.

Compare:

  1. An Option[T] type containing the values Some(t), for each value t in T, and the additional value None.
  2. A List[T] type containing objects built from two subclasses (and their respective constructors) Cons[T](h: T, t: List[T]) and Nil: these represent immutable lists whose elements have type T. Note that here the method Nil.isEmpty returns false as you mentioned.

None indicates the absence of a value, while Nil indicates an empty list.

So an optional value None is not the same as an empty list Nil.

NOTE

If you need to test if an optional value is defined or not you can use pattern matching:

class MyClass(f: Option[String])
{
  val field = f
}

and then, to test if the field of an instance obj of MyClass is defined:

obj.field match
{
  case Some(s) => /* do something with s */
  case None    => /* ... */
}

I also attended the same Scala course and I agree with e-i-s that you should use the Option[T] type for implementing an optional field.

The pattern involving

  • a trait with method isEmpty
  • several classes or objects extending the trait
  • a unique object whose method isEmpty always returns true

is used to implement sequence-like immutable structures such as lists, streams, and so on.

Compare:

  1. An Option[T] type containing the values Some(t), for each value t in T, and the additional value None.
  2. A List[T] type containing objects built from two subclasses (and their respective constructors) Cons[T](h: T, t: List[T]) and Nil: these represent immutable lists whose elements have type T. Note that here the method Nil.isEmpty returns true.

None indicates the absence of a value, while Nil indicates an empty list.

So an optional value None is not the same as an empty list Nil.

NOTE

If you need to test if an optional value is defined or not you can use pattern matching:

class MyClass(f: Option[String])
{
  val field = f
}

and then, to test if the field of an instance obj of MyClass is defined:

obj.field match
{
  case Some(s) => /* do something with s */
  case None    => /* ... */
}
added 24 characters in body
Source Link
Giorgio
  • 19.8k
  • 16
  • 89
  • 137

I also attended the same Scala course and I agree with e-i-s that you should use the Option[T] type for implementing an optional field.

The pattern involving

  • a trait with method isEmpty
  • several classes or objects extending the trait
  • a unique object whose method isEmpty always returns true

is used to implement sequence-like immutable structures such as lists, streams, and so on.

Compare:

  1. An Option[T] type containing the values Some(t), for each value t in T, and the additional value None.
  2. A List[T] type containing objects built from two subclasses (and their respective constructors) Cons[T](h: T, t: List[T]) and Nil: these represent immutable lists whose elements have type T. Note that here the method Nil.isEmpty returns false as you mentioned.

None indicates the absence of a value, while Nil indicates an empty list.

So an optional value None is not the same as an empty list Nil.

NOTE

If you need to test if an optional value is defined or not you can use pattern matching:

class MyClass(f: Option[String])
{
  val field: = f
}

and then, to test if the field of an instance obj of MyClass is defined:

obj.field match
{
  case Some(s) => /* do something with s */
  case None    => /* ... */
}

I also attended the same Scala course and I agree with e-i-s that you should use the Option[T] type for implementing an optional field.

The pattern involving

  • a trait with method isEmpty
  • several classes or objects extending the trait
  • a unique object whose method isEmpty always returns true

is used to implement sequence-like immutable structures such as lists, streams, and so on.

Compare:

  1. An Option[T] type containing the values Some(t), for each value t in T, and the additional value None.
  2. A List[T] type containing objects built from two subclasses (and their respective constructors) Cons[T](h: T, t: List[T]) and Nil: these represent immutable lists whose elements have type T.

None indicates the absence of a value, while Nil indicates an empty list.

So an optional value None is not the same as an empty list Nil.

NOTE

If you need to test if an optional value is defined or not you can use pattern matching:

class MyClass(f: Option[String])
{
  val field: f
}

and then, to test if the field of an instance obj of MyClass is defined:

obj.field match
{
  case Some(s) => /* do something with s */
  case None    => /* ... */
}

I also attended the same Scala course and I agree with e-i-s that you should use the Option[T] type for implementing an optional field.

The pattern involving

  • a trait with method isEmpty
  • several classes or objects extending the trait
  • a unique object whose method isEmpty always returns true

is used to implement sequence-like immutable structures such as lists, streams, and so on.

Compare:

  1. An Option[T] type containing the values Some(t), for each value t in T, and the additional value None.
  2. A List[T] type containing objects built from two subclasses (and their respective constructors) Cons[T](h: T, t: List[T]) and Nil: these represent immutable lists whose elements have type T. Note that here the method Nil.isEmpty returns false as you mentioned.

None indicates the absence of a value, while Nil indicates an empty list.

So an optional value None is not the same as an empty list Nil.

NOTE

If you need to test if an optional value is defined or not you can use pattern matching:

class MyClass(f: Option[String])
{
  val field = f
}

and then, to test if the field of an instance obj of MyClass is defined:

obj.field match
{
  case Some(s) => /* do something with s */
  case None    => /* ... */
}
added 1 characters in body
Source Link
Giorgio
  • 19.8k
  • 16
  • 89
  • 137
Loading
Source Link
Giorgio
  • 19.8k
  • 16
  • 89
  • 137
Loading