Skip to main content
added 562 characters in body; added 429 characters in body
Source Link
Adamátor
  • 8.3k
  • 2
  • 15
  • 28

Nim

Overriding the built-in and:

proc `and`(x, y: bool): bool = false let a = true b = true if a: echo("a is true") if b: echo("b is true") if a and b: echo("a and b is true") 

Attempt This Online!

Using a term-rewriting template:

template xand{`and`(x, y)}(x, y: bool): bool = false let a = true b = true if a: echo("a is true") if b: echo("b is true") if a and b: echo("a and b is true") 

Attempt This Online!

Using a custom type:

type XandBool = enum xFalse xTrue converter toBool(x: XandBool): bool = case x of xFalse: false of xTrue: true proc `and`(x, y: XandBool): XandBool = xFalse let a = xTrue b = xTrue if a: echo("a is true") if b: echo("b is true") if a and b: echo("a and b is true") 

Attempt This Online!

Nim

type XandBool = enum xFalse xTrue converter toBool(x: XandBool): bool = case x of xFalse: false of xTrue: true proc `and`(x, y: XandBool): XandBool = xFalse let a = xTrue b = xTrue if a: echo("a is true") if b: echo("b is true") if a and b: echo("a and b is true") 

Attempt This Online!

Nim

Overriding the built-in and:

proc `and`(x, y: bool): bool = false let a = true b = true if a: echo("a is true") if b: echo("b is true") if a and b: echo("a and b is true") 

Attempt This Online!

Using a term-rewriting template:

template xand{`and`(x, y)}(x, y: bool): bool = false let a = true b = true if a: echo("a is true") if b: echo("b is true") if a and b: echo("a and b is true") 

Attempt This Online!

Using a custom type:

type XandBool = enum xFalse xTrue converter toBool(x: XandBool): bool = case x of xFalse: false of xTrue: true proc `and`(x, y: XandBool): XandBool = xFalse let a = xTrue b = xTrue if a: echo("a is true") if b: echo("b is true") if a and b: echo("a and b is true") 

Attempt This Online!

Source Link
Adamátor
  • 8.3k
  • 2
  • 15
  • 28

Nim

type XandBool = enum xFalse xTrue converter toBool(x: XandBool): bool = case x of xFalse: false of xTrue: true proc `and`(x, y: XandBool): XandBool = xFalse let a = xTrue b = xTrue if a: echo("a is true") if b: echo("b is true") if a and b: echo("a and b is true") 

Attempt This Online!